python 3.6.8
/tmp/t1.txt如下:
Nearly four-fifths of
U.S. oil and gas produc-
tion in the Gulf of Mexico
上例只是一个示例,一个共三小行的小文本文件。
代码如下:
load_in=set()
with open('/tmp/t1.txt', 'r') as file:
for line in file:
for word in line.split():
print(word)
load_in(word)
代码运行正常。唯一不足的是无法正确认别并组合句末被破折号拆分的词,如上例的produc-tion。
求解决方案。
非句末的含破折号的词,如four-fifths,不用管。
多谢!
for word in line.split():
尝试改成:
for word in line.split("-\n"):