擅长:python、mysql、java
<p><strong>代码点数:</strong></p>
<ul>
<li>可以直接使用文件句柄逐行迭代。不需要在列表中使用<code>fp.readlines()</code>保存文件数据,然后进行迭代。你知道吗</li>
<li>一旦找到所需的类别,您就可以直接附加完整的行。这就是为什么你得到了错误的输出。您只需要拆分行并保存第一个元素。你知道吗</li>
<li>我不明白你为什么用<code>break</code>。你知道吗</li>
</ul>
<p><strong>工作代码:</strong></p>
<pre><code>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)
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>C:\Users\dinesh_pundkar\Desktop>python c.py
['abcd.asxs', 'megaten4.koryaku-memo.xyz', 'adwdbk.uyequ', 'tytqyw.sdfhgwq']
C:\Users\dinesh_pundkar\Desktop>
</code></pre>