我有一个以下模式的文件:
"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-备忘录.xy.“字母表”
““tytqyw.sdfhgwq公司.“字母表”
代码点数:
fp.readlines()
保存文件数据,然后进行迭代。你知道吗break
。你知道吗工作代码:
输出:
将
important.append(line)
更改为:if line.strip().endswith('"alphabets"'): important.append(line.split(' ')[0].strip('"').strip('''))
相关问题 更多 >
编程相关推荐