我想知道如何修改文本文件。假设我有.txt之类的文件
1, yes, cool being
how, are you doing
see, you, you see them
我想创建一个新的/修改过的文件,其中
1
yes, cool being
how
are you doing
see
you, you see them
我尝试了以下代码
wordlist = []
with open('scores.txt') as f:
wordlist = [line.split(None, 1)[0] for line in f]
print(wordlist[0]) # trying to see if the output was the way i wanted
因此,您希望在第一次出现
,
时split
,并与一个换行符(\n
)合并,然后再次将“段落”与一个换行符合并产生
(此解决方案建议不会按照您的要求在字符串末尾追加换行符。如果您正在运行windows,请将
\n
替换为\r\n
,在MAC上,将\r
替换为。)尝试:
相关问题 更多 >
编程相关推荐