从另一个文件中获取特定文本(python)

时间:2016-10-17 20:28:45

标签: python

我有一个以下模式的文件:

"abcd.asxs." "alphabets"
"wedf.345.po&%12." "numbers"
"xyhd.iu*u." "characters"
"megaten4.koryaku-memo.xyz." "alphabets"
"adwdbk.uyequ." "alphabets"
"233432.2321." "numbers"
"tytqyw.sdfhgwq." "alphabets"

我想要类似的东西:

string[0]=abcd.asxs
string[1]=megaten4.koryaku-memo.xyz
string[2]=tytqyw.sdfhgwq
and so on....

我写的代码是:

 #!/usr/bin/python

 import re

 important = []
 needed_categories = ["alphabets"]

 with open('file.txt') as fp:
        rec=fp.readlines()

 for line in rec:
        for category in needed_categories:
                if category in line:
                        important.append(line)
                        break
 print("\n".join(important))

输出我得到:

“abcd.asxs”。 “字母”

“megaten4.koryaku-memo.xy”。 “字母”

“tytqyw.sdfhgwq”。 “字母”

2 个答案:

答案 0 :(得分:0)

代码点数:

  • 您可以直接使用文件句柄逐行迭代。无需使用列表中的fp.readlines()保存文件数据,然后进行迭代。
  • 找到needed_category后,您将直接附加完整的行。这就是你输出错误的原因。您需要拆分行并仅保存第一个元素。
  • 不明白你使用break的原因。

工作代码:

important = []
needed_categories = ["alphabets"]

with open('a.txt') as fp:
    for line in fp:
        temp = []
        for category in needed_categories:
            if category in line:
                temp = line.split()
                important.append(temp[0].replace('"','').strip("."))
print((important)

<强>输出:

C:\Users\dinesh_pundkar\Desktop>python c.py
['abcd.asxs', 'megaten4.koryaku-memo.xyz', 'adwdbk.uyequ', 'tytqyw.sdfhgwq']

C:\Users\dinesh_pundkar\Desktop>

答案 1 :(得分:0)

important.append(line)更改为:

if line.strip().endswith('"alphabets"'): important.append(line.split(' ')[0].strip('"').strip('''))