拆分文本文件,whit space pronlem

时间:2017-07-20 19:43:36

标签: python

我已经成功地将文本文件拆分成我想要的句子,用逗号分割文本文件。

文本文件:

You will find love in the next week, You will die in the next minute, You will get married and have 7 kids, You will end up in prison

代码:

from random import sample

Future = open('Future.txt', 'r')
for line in Future:
    Truth = line.split(",")
    field1 = Truth[0]
    print (field1)

然而,当我运行程序时,我得到了文本文件的第一行,就像预期的那样,然后就像4条白线一样。 在理想的场景之后,如何阻止白线出现?

1 个答案:

答案 0 :(得分:0)

你可以像这样跳过空行:

for line in Future:
    if not line.strip():
        continue
    Truth = line.split(",")