Python - 添加时删除目标的最后一行
Python - removing last line of target when appending
我使用 RTF 模板收集数据:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be %(data)s
}
我有一个单独的附加操作模板,就像上面没有第一行的模板。
挑战在于,创建文件后,稍后可能会有相同文件名的其他数据。可以将数据附加到文件,但如果文件存在,我还没有找到摆脱“}”EOF 语法的方法。这是在不删除最后一行的情况下附加数据的代码:
rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").read() # rtf append template
data_source = {"data": "test_data"} # just a string to keep this simple
if os.path.isfile(rtf_filename) is True: # RTF file might exist
with open(rtf_filename, "a") as f:
f.write(template_append % data_source)
else:
with open(rtf_filename, "w") as f:
f.write(template % data_source)
以上代码的文件输出:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be test_data
}\f0\fs28 Here be test_data # line where "}" should be removed
}
感谢您的帮助。
这个代码
with open(rtf_filename, "a") as f:
f.write(template_append % data_source)
直接附加到新打开的文件的末尾。
由于这些看起来是小文件,最简单的方法是将文件读入列表,删除列表末尾的结束符 }
,将新数据追加到列表中,最后将文件写回。您可以就地覆盖文件,或使用临时文件,然后用临时文件替换原始文件,如下所示:
import shutil
from tempfile import NamedTemporaryFile
rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").readlines()
data_source = {'data': 'test_data'}
if os.path.isfile(rtf_filename) is True: # RTF file might exist
with open(rtf_filename) as rtf_file, NamedTemporaryFile(dir='.', delete=False) as tmp_file:
lines = rtf_file.readlines()[:-1] # reads all lines and truncates the last one
lines.extend([s % data_source for s in template_append])
tmp_file.writelines(lines)
shutil.move(tmp_file.name, rtf_filename)
else:
with open(rtf_filename, "w") as f:
f.write(template % data_source)
我使用 RTF 模板收集数据:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be %(data)s
}
我有一个单独的附加操作模板,就像上面没有第一行的模板。
挑战在于,创建文件后,稍后可能会有相同文件名的其他数据。可以将数据附加到文件,但如果文件存在,我还没有找到摆脱“}”EOF 语法的方法。这是在不删除最后一行的情况下附加数据的代码:
rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").read() # rtf append template
data_source = {"data": "test_data"} # just a string to keep this simple
if os.path.isfile(rtf_filename) is True: # RTF file might exist
with open(rtf_filename, "a") as f:
f.write(template_append % data_source)
else:
with open(rtf_filename, "w") as f:
f.write(template % data_source)
以上代码的文件输出:
{\rtf1\ansi\deff0 {\fonttbl {\f0 Monotype Corsiva;}}
\f0\fs28 Here be test_data
}\f0\fs28 Here be test_data # line where "}" should be removed
}
感谢您的帮助。
这个代码
with open(rtf_filename, "a") as f:
f.write(template_append % data_source)
直接附加到新打开的文件的末尾。
由于这些看起来是小文件,最简单的方法是将文件读入列表,删除列表末尾的结束符 }
,将新数据追加到列表中,最后将文件写回。您可以就地覆盖文件,或使用临时文件,然后用临时文件替换原始文件,如下所示:
import shutil
from tempfile import NamedTemporaryFile
rtf_filename = time.strftime("%d") + ".rtf" # rtf filename in date format
template = open("template.rtf").read() # rtf template
template_append = open("template_append.rtf").readlines()
data_source = {'data': 'test_data'}
if os.path.isfile(rtf_filename) is True: # RTF file might exist
with open(rtf_filename) as rtf_file, NamedTemporaryFile(dir='.', delete=False) as tmp_file:
lines = rtf_file.readlines()[:-1] # reads all lines and truncates the last one
lines.extend([s % data_source for s in template_append])
tmp_file.writelines(lines)
shutil.move(tmp_file.name, rtf_filename)
else:
with open(rtf_filename, "w") as f:
f.write(template % data_source)