jtk*_*kiv 2 python text line text-files python-3.x
我有一些数据以这种格式存储在.txt文件中:
----------|||||||||||||||||||||||||-----------|||||||||||
1029450386abcdefghijklmnopqrstuvwxy0293847719184756301943
1020414646canBeFollowedBySpaces 3292532113435532419963
Run Code Online (Sandbox Code Playgroud)
不要问......
我有很多这样的行,我需要一种方法来添加更多的数字到特定行的末尾.
我已经编写了代码来找到我想要的行,但是我很难过如何在它的末尾添加11个字符.我环顾四周,这个网站对我遇到的其他一些问题很有帮助,但我似乎无法找到我需要的东西.
重要的是,该行保留其在文件中的位置,以及其当前顺序的内容.
使用python3.1,你怎么会这样:
1020414646canBeFollowedBySpaces 3292532113435532419963
Run Code Online (Sandbox Code Playgroud)
成
1020414646canBeFollowedBySpaces 329253211343553241996301846372998
Run Code Online (Sandbox Code Playgroud)
作为一般原则,没有在文本文件中间"插入"新数据的快捷方式.您需要在新文件中复制整个原始文件,并在途中修改所需的文本行.
例如:
with open("input.txt") as infile:
with open("output.txt", "w") as outfile:
for s in infile:
s = s.rstrip() # remove trailing newline
if "target" in s:
s += "0123456789"
print(s, file=outfile)
os.rename("input.txt", "input.txt.original")
os.rename("output.txt", "input.txt")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |