![](/img/trans.png)
[英]How to load .txt file as a numpy array such that it only reads in certain lines?
[英]replace specific string in a txt file, but only in certain lines
txt 文件有 40000 行。 每行都是逗号分隔的数字。 我想删除 36000 到 39000 行中的特定数字。例如数字 233。但我不想从数字 23341 中删除字符串。
到目前为止,这是我的代码:
with open("example.txt","r") as file:
newline =[]
i = 0
for l in file.readlines():
if i>=36000 and i<=39000:
newline.append(word.replace("233",""))
else:
newline.append(word.replace("233","233"))
i = i + 1
with open("example.txt","w") as file:
for line in newline:
f.writelines(line)
有没有更优雅的方法来解决这个问题?
您可以在此处使用正则表达式替换:
for line in file.readlines():
if i >= 36000 and i <= 39000:
line = re.sub(r',?\b233\b,?', ',', line).strip(',')
newline.append(line)
i = i + 1
上面的正则表达式逻辑专门将值233
作为 CSV 值。 模式和替换确保生成的 CSV 没有空值或悬空逗号。
遍历一个大文本文件,将每一行附加到一个列表中以进一步覆盖整个文件——这绝对是低效的方法,请使用fileinput
模块和预编译(使用re.compile
)正则表达式模式:
import fileinput, re
with fileinput.input('example.txt', inplace=True, encoding='utf-8') as f:
pat = re.compile(r'\b233\b')
for i, line in enumerate(f):
if i >= 36000 and i <= 39000:
line = pat.sub('', line)
print(line, end='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.