我有一个文本文件,它在破折号“-”之前有一个公司名称,我想查找该公司名称示例:TELSTRA - EV 12M FWD
我只找到了访问仪表板的方法
import re
hand = open('Companies.txt')
content = hand.read()
hand.close()
for line in content:
if re.search(' -', line) :
print(line)
我希望输出是 TELSTRA。
最佳答案
您可以尝试在此处使用 re.findall
,其模式为 (\S+)(?=\s*-)
:
input = "TELSTRA - EV 12M FWD"
matches = re.findall(r'(\S+)(?=\s*-)', input)
print(matches)
输出:
['TELSTRA']
关于python - 如何在文本文件中查找字符之前的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56258833/