如何使用Python在文本文件中写多行?
您可以使用写功能通过用'\ n'分隔行来写多行。
例如
line1 = "First line"line2 = "Second line"
line3 = "Third line"
with open('my_file.txt','w') as out:
out.write('{}\n{}\n{}\n'.format(line1,line2,line3))
或者,您可以使用writelines函数来编写这些行。
例如
line1 = "First line"line2 = "Second line"
line3 = "Third line"
with open('my_file.txt','w') as out:
out.writelines([line1, line2, line3])
以上是 如何使用Python在文本文件中写多行? 的全部内容, 来源链接: utcz.com/z/350285.html