如果我有一个包含大量数据的.txt文件,那么假设当前格式如下的电影评论:
1“达芬奇密码书”真是太棒了。这是我读过的第一本克莱夫·库斯勒,但即使是像遗物和达芬奇密码这样的书也比这更可信。我非常喜欢达芬奇密码。我非常喜欢达芬奇密码。我喜欢“达芬奇密码”,但它最终似乎并不是属于自己的。1这一点都不夸张),在午夜时分,我们去沃尔玛买了达芬奇密码,这当然令人惊讶。
我如何修改该文件或将其内容写入一个新文件,以便在每句话结束后,下一行开始于新行而不是同一行?
发布于 2018-04-08 21:07:59
您可以在"."拆分文本,然后使用字符串格式:
import re
new_s = ['{}\n'.format(i) for i in re.split('\.\s*', open('filename.txt').read())]
with open('movie_listing.txt', 'a') as f:
f.write(''.join(new_s))输出(以movie_listing.txt表示):
1 The Da Vinci Code book is just awesome
1 this was the first clive cussler i've ever read, but even books like Relic, and Da Vinci code were more plausible than this
1 i liked the Da Vinci Code a lot
1 i liked the Da Vinci Code a lot
1 I liked the Da Vinci Code but it ultimatly didn't seem to hold it's own
1 that's not even an exaggeration ) and at midnight we went to Wal-Mart to buy the Da Vinci Code, which is amazing of coursehttps://stackoverflow.com/questions/49722777
复制相似问题