我有一些包含很多行的文件。在每一行的开头都有一些数字,其中有#34 ;;"它们之间。如何删除这些数字和";"? (我测试拆分将数字放在一起所以我可以删除它们但是当我使用拆分时,数字旁边的单词会随附它们,当我删除数字时,它们也会删除。但我不想删除删除单词。只需要数字和";")。或者在记事本++中有办法吗?
示例文件: https://www.dropbox.com/s/yvgc659f9rrfhop/N.txt?dl=0
file = "c:/Python34/N.txt"
h = ["1","2","3","4","5","6","7","8","9","0", ";"]
with open (file) as f:
for line in f:
for i in h:
if i in line:
line.replace(i, "")
print (line)
with open ("new.txt", "w") as f2:
f2.write(line)
答案 0 :(得分:1)
正则表达式可以解决这个问题:
import re
file = 'c:/Python34/N.txt'
with open(file) as f:
contents = re.sub(r'\d+;', '', f.read())
with open('new.txt', 'w') as f2:
f2.write(contents)