在 test.txt 中,我有 2 行句子。
The heart was made to be broken.
There is no surprise more magical than the surprise of being loved.
编码:
import re
file = open('/test.txt','r')#specify file to open
data = file.readlines()
file.close()
for line in data:
line_split = re.split(r'[ \t\n\r, ]+',line)
print line_split
代码的结果:
['The', 'heart', 'was', 'made', 'to', 'be', 'broken.', '']
['There', 'is', 'no', 'surprise', 'more', 'magical', 'than', 'the', 'surprise', 'of', 'being', 'loved.']
如何只打印单词?(见第一句)期望结果:
['The', 'heart', 'was', 'made', 'to', 'be', 'broken.']
['There', 'is', 'no', 'surprise', 'more', 'magical', 'than', 'the', 'surprise', 'of', 'being', 'loved.']
有什么建议吗?