简体   繁体   English

在python中使用方法startswith()和re.findall()提取确切的单词

[英]use of method startswith() and re.findall() in python to extract exact words

I have a text file like below, 我有一个如下的文本文件,

&attri 'abc' real
&attri 'cde' real
&attri 'efg' {'0', '1'}
&attri 'ghi_jkl' real
&attri 'lmn' real
&attri 'nop' real
&attri 'pqr_stu_uvw' real
&attri 'xy_z' {'0', '1'}

I want to extract all the words 'abc', 'cde', 'efg', 'ghi_jkl', 'lmn', 'nop', 'pqr_stu_uvw', 'xy_z' from that file. 我想从该文件中提取所有单词'abc', 'cde', 'efg', 'ghi_jkl', 'lmn', 'nop', 'pqr_stu_uvw', 'xy_z'

I wrote the following code, but it prints just [] . 我编写了以下代码,但仅输出[]

import re

col = []

with open('text1.txt', 'r') as f:
    lines=f.readlines()
    for line in lines:
        l = line.strip()
        if l.startswith('&attri'):
            col.append(re.findall(r"'(.{1})'", l))
print(col)  

Thank you for helping me with this. 谢谢您的帮助。

You don't need regex here considering that you always need to extract second word of each line out of file. 考虑到您始终需要从文件中提取每一行的第二个单词,因此在这里不需要正则表达式。 Use split() and extract second split: 使用split()并提取第二个split:

col = []

with open('text1.txt', 'r') as f:
    for line in f:
        if line.strip().startswith('&attri'):
            col.append(line.split()[1].strip("'"))

print(col)

Replace 2 lines with 将2行替换为

       if l.startswith('&attri'):
            col.append(l.split()[1].strip("'"))

Try this: 尝试这个:

import re

s = '''&attri 'abc' real
&attri 'cde' real
&attri 'efg' {'0', '1'}
&attri 'ghi_jkl' real
&attri 'lmn' real
&attri 'nop' real
&attri 'pqr_stu_uvw' real
&attri 'xy_z' {'0', '1'}'''


print(re.findall("'[a-zA-Z_]*'",s)

Result: 结果:

["'abc'", "'cde'", "'efg'", "'ghi_jkl'", "'lmn'", "'nop'", "'pqr_stu_uvw'", "'xy_z'"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM