[英]How can I convert a string to multiple lines?
I'm trying to convert a long list of words into a multiple line.txt file我正在尝试将一长串单词转换为多 line.txt 文件
Example:例子:
longStringWithWords = 'THESE ARE WORDS THAT I LIKE'
into进入
THESE
ARE
WORDS
THAT
I
LIKE
I've tried this:我试过这个:
differentVar = longStringWithWords.split(' ')
file = open('filename.txt', 'a')
for i in differentVar:
file.write(i)
file.write('\n')
It's still not getting what I want.它仍然没有得到我想要的。 What am I missing?
我错过了什么?
Thanks in advance;)提前致谢;)
You forgot to close the file:您忘记关闭文件:
file.close()
However, this is more efficient:但是,这更有效:
longStringWithWords = 'THESE ARE WORDS THAT I LIKE'
longStringWithWords = longStringWithWords.replace(' ', ' \n')
with open('filename.txt', 'a') as f:
f.write(longStringWithWords)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.