我有一个以下模式的文件:
"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”。 “字母”
答案 0 :(得分:0)
代码点数:
fp.readlines()
保存文件数据,然后进行迭代。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('''))