我有一个如下所示的文件:
Breve, a writ; used more frequently in the plural brevia.
Brevia magistralia, official writs framed by the clerks in
chancery to meet new injuries, to which the old forms of action
were inapplicable. Sea Trespass on the case. Brevia testata,
short attested memoranda, originally introduced to obviate the
uncertainty arisina; from parol feoffments, hence modern con-
veyances have gradually arisen.
我想提取每行中第一个逗号(,)之前出现的单词
预期输出:
Breve
Brevia magistralia
chancery to meet new injuries
were inapplicable. Sea Trespass on the case. Brevia testata
short attested memoranda
uncertainty arisina; from parol feoffments
my code:
with open('test.txt','r') as file:
for line in file:
print(line[0:line.find(',')])
output:
Breve
任何帮助表示赞赏
为什么需要正则表达式?str.split
应该足够好了。
with open('test.txt','r') as file:
for line in file:
text = line.split(',', 1)[0] # add nsplits = 1 for efficiency
... # do something with text
但是,如果您确实需要正则表达式,您可以使用类似以下内容的内容:
for line in file:
m = re.match('[^,]+', line)
if m:
text = m.group(0)
Where [^,]+
匹配从一开始就不是逗号的任何内容 (credits https://stackoverflow.com/questions/46190206/python-extract-word-before-1st-string-occurrence-in-each-line-in-text-file/46190293?noredirect=1#comment79342716_46190293).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)