将段落写入文件

时间:2011-03-08 00:06:31

标签: python

好的,这很简单,我想在文件中写一段巨大的文字。我知道我可以做到

f=open("blah.txt",'w')
f.write("blah blah \n")
f.write("bla blah blah ")
f.close()

但是什么是更优雅的解决方案?

2 个答案:

答案 0 :(得分:17)

f.write("""I’ve got a lovely bunch of coconuts
There they are all standing in a row
Big ones, small ones, some as big as your head
Give them a twist a flick of the wrist
That’s what the showman said""")

答案 1 :(得分:3)

您可以像这样使用3引号字符串声明。

>>> a = '''blah blah blah blah
blah blah blah blah
blah blah blah blah'''
>>> print(a)
blah blah blah blah
blah blah blah blah
blah blah blah blah