我的代码有问题。我正在尝试替换文件中的模式。首先,我在打开文件的数量上有一个错误,因为我忘记关闭我的文件。但现在,我的代码中有f.close((,我有以下错误:
ValueError: I/O operation on closed file.
在这里你可以找到我代码的一部分。有人知道怎么了?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import shutil
from tempfile import mkstemp
infile = 'test_file.txt'
year = int('2009')
month = int('1')
day = int('10')
#################################################################################
def sed(pattern, replace, source):
"""
Reads a source file and writes the destination file.
In each line, replaces pattern with replace.
Args:
pattern (str): pattern to match (can be re.pattern)
replace (str): replacement str
source (str): input filename
"""
fin = open(source, 'r')
fd, name = mkstemp()
fout = open(name, 'w')
for line in fin:
out = re.sub(pattern, replace, line)
fout.write(out)
fin.close()
fout.close()
shutil.move(name, source)
#################################################################################
def main():
"""
Replace all year-month-days which have a possible wrong pattern
"""
for i in range (6):
for j in range (12):
for k in range (22):
Year = year + i; Month = month + j; Day = day + k
pattern = '%s %s%s' %(Year, Month, Day)
replace = '%s %s %s' %(Year, Month, Day)
sed(pattern, replace, infile)
#################################################################################
if __name__ == "__main__":
main()
###### END
非常感谢。
我将此作为另一个答案发布,因为它显然不同。
由于sed
函数打开过多文件时出现问题,我尝试编写一些尽可能少地打开文件的程序。此外,你说你要编辑的文件很大,所以我避免直接把它读入内存。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
input_file = 'test_file.txt'
output_file = 'result_file.txt'
def main():
pattern = r"(d{4}) (d{1,2})(d{2})"
replace = r"1 2 3"
with open(input_file, "r") as inp:
with open(output_file, "w") as oup:
for line in inp:
sub = re.sub(pattern, replace, line)
oup.write(sub)
if __name__ == "__main__":
main()
我认为问题在于您错误地使用了sed
函数。在main
函数中打开要编辑的文件,然后在sed
函数中再次打开(然后关闭(。
看起来sed
函数应该用于整个文件,而不仅仅是一行。
如果你把main
函数编辑成这样,它应该可以工作(如果不能,请评论出了什么问题(:
def main():
"""
Replace all year-month-days which have a possible wrong pattern
"""
occurrences = 999 # arbitray number. If you know the exact number, you may want to edit this.
for i in range (6):
for j in range (12):
for k in range (22):
Year = year + i; Month = month + j; Day = day + k
pattern = '%s %s%s' %(Year, Month, Day)
replace = '%s %s %s' %(Year, Month, Day)
sed(pattern, replace, infile, count=occurrences)