file.write在python中自动附加一个新行



我正在尝试在python中用一个单词附加多个单词,以将其写入文件,如下所示:

out_file.write(words+'Positive')

我在这里不使用n。但输出结果是 2 行,如下所示:

It is awesome awesome.
Positive

我期待的是It is awesome. Positive.请帮忙。

试试这个:

string_with_2_lines = words + 'Positive'
string_altered = string_with_2_lines.replace('n',' ')
>>>It is awesome. Positive

下面是一个示例:

words = 'It is awesome awesome.'
with open('sample.txt', 'w') as file_object:
file_object.write(words + ' ' + 'Positive')

以下是您在.txt中的输出:

It is awesome awesome. Positive

最新更新