Python implements the method of appending text to a specified line in txt

不言
Release: 2018-05-02 16:09:11
Original
9808 people have browsed it

This article mainly introduces the method of appending text to a specified line in txt in python. It has a certain reference value. Now I share it with you. Friends in need can refer to it

As shown below:

fp = file('data.txt')
lines = []
for line in fp: 
  lines.append(line)
fp.close()
 
lines.insert(1, 'a new line') # 在第二行插入
s = '\n'.join(lines)
fp = file('data.txt', 'w')
fp.write(s)
fp.close()
Copy after login

Related recommendations:

Python implements the generation of strips for images in files Labeled txt file method

Python method to merge all txt files in the same folder

##

The above is the detailed content of Python implements the method of appending text to a specified line in txt. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!