[英]How to write multiple output lines to a text file without overwriting previous multiple lines in python?
问题陈述:
Ubuntu 18.04 与 python 3.6.9
python 3.6 脚本“generator.py”不断覆盖“combined_output_file.txt”中以前的 output 行
'Textgenerator.py' 是导入上面'generator' 的主脚本,它处理所有打开的文件读取(r)和写入(w+)函数。
python TextGenerator.py -t input_file_1.txt -c input_file_2.txt -o combined_output_file.txt
input_file_1.txt 的内容:
这是第一个输入文件的第 1 行。
这是第一个输入文件的第 2 行。
这是第一个输入文件的第 3 行。
等等
input_file_2.txt 的内容:
这是 SECOND 输入文件的第 1 行。
这是 SECOND 输入文件的第 2 行。
这是 SECOND 输入文件的第 3 行。
等等
更正“combined_output_file.txt”的 output 格式
这是第一个输入文件的第 1 行。
这是 SECOND 输入文件的第 1 行。
1023 个单词通过 gpt2 语言 model 基于上述行生成......等。
这是第一个输入文件的第 2 行。
这是 SECOND 输入文件的第 2 行。
1023 个新词通过 gpt2 语言 model 基于上述行生成......等。
这是第一个输入文件的第 3 行。
这是 SECOND 输入文件的第 3 行。
1023 MORE NEW words 通过 gpt2 语言 model 基于以上行生成...等。
这是第一个输入文件的第 3 行。
这是 SECOND 输入文件的第 3 行。
1023 个单词通过 gpt2 语言 model 基于上述行生成......等。
尝试的解决方案:
将 w+ 切换为 a+ 并添加 '\n' 与上面相同的覆盖 output。
def write_sample_to_file(self, filename, sample):
"""Write a given sample to a file specified by the filename."""
with open(filename, 'a+', errors='surrogateescape', encoding='utf-8') as f:
f.write(sample + '\n') # added and changed w+ to a+ same output
不确定这是否是正确的方法。 这个可以应用吗?
导入操作系统
从 gpt2handler 导入 Gpt2Handler ''' 仅用于 model '''
def generate_from_files(self,
title_filename,
content_filename=None,
num_samples=1,
print_output=False,
output_file=None,
num_words=1023):
"""Read the title from a file and initial content from another file then use gpt2 to generate an article
and return it as a single string."""
with open(title_filename, 'r', errors='surrogateescape') as title_file:
for line in title_file: # added for 'input_file_1.txt'
title = line # reading to end of input_file_1 but copying over each output line 1
if content_filename:
with open(content_filename, 'r', errors='surrogateescape') as content_file:
for line in content_file: #added for 'input_file_1.txt'
initial_content = line # reading to end of input_file_1 but copying over each output line 1
else:
initial_content = ''
return self.generate(title, initial_content, num_samples, print_output, output_file, num_words)
def generate(self,
title,
initial_content=None,
num_samples=1,
print_output=False,
output_file=None,
num_words=1023):
"""Use gpt2 to generate an article based on a given title and initial content."""
if not initial_content:
initial_content = ''
samples = Gpt2Handler.get_instance().generate_as_tuple(title, initial_content, num_samples, num_words)
samples_str = [sample[0] + '\n' + sample[1] for sample in samples]
if print_output: # Print each article to the console is specified to
for sample in samples_str:
print(sample)
if output_file: # Write each of the samples to their own file if a base filename is specified
self.write_samples_to_file(output_file, samples_str)
return samples_str
def write_samples_to_file(self, filename, samples):
"""Write the given samples to a file. If there is more than one, write each to its own file."""
if len(samples) == 1:
self.write_sample_to_file(filename, samples[0])
else:
base, extension = os.path.splitext(filename)
for i in range(len(samples)):
new_filename = base + str(i) + extension
self.write_sample_to_file(new_filename, samples[i])
def write_sample_to_file(self, filename, sample):
"""Write a given sample to a file specified by the filename."""
with open(filename, 'w+', errors='surrogateescape', encoding='utf-8') as f:
f.write(sample + '\n') # added and changed w+ to a+ same output
改变
def write_sample_to_file(self, filename, sample):
"""Write a given sample to a file specified by the filename."""
with open(filename, 'w+', errors='surrogateescape', encoding='utf-8') as f:
f.write(sample + '\n') # added and changed w+ to a+ same output
至
def write_sample_to_file(self, filename, sample):
"""Write a given sample to a file specified by the filename."""
with open(filename, 'a', errors='surrogateescape', encoding='utf-8') as f:
f.write(sample + '\n') # added and changed w+ to a+ same output
你想用'a'文件而不是写入它来理解它,例如,如果你with open(index.html, w)
它会检查 index.html 在与你的 Z23EEEB4347BDD26BFC6B7 文件相同的文件夹中是否存在删除它并创建一个新的 index.html 并将任何内容写入其中,因此您必须with open(index.html, a)
来查找现有文件然后添加到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.