[英]Convert spaces to new lines in a text file
我正在尝试将文本文件中的所有空格转换为新行,这样我最后就会得到文本中所有单词的列表。
with open('keywords.txt', 'w+') as g:
replace = string.replace(" ","\n")
replace.writelines
可悲的是,这对我不起作用。
我愿意接受任何提示或想法,我不敢相信我无法让需要 3-5 行代码的东西工作。
'w+' 将清空您的文件,您永远不会读入当前内容,并且 string.replace 不会那样工作。
with open('keywords.txt', 'r+') as g:
s = g.read()
s = s.replace(" ", "\n")
g.seek(0)
g.truncate()
g.write(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.