在文本文件的所有行中搜索字符串:Python
在这里遇到问题,希望能得到一些帮助。
我有一个文本文件,每行都有一个 ID 号和一组“描述符”。每行的描述符可能是唯一的,也可能不是唯一的(它们可以在整个文档中多次使用)。
我基本上想识别包含某个描述符的所有 ID 号...我的代码正在运行,但它只找到该描述符的第一次出现,而不是全部。有什么快速解决办法吗?
所有描述符都已在列表中。 文本文件示例:
ID_45555 (tab) some irrelevant data (tab) **DESCRIPTOR1** DESCRIPTOR2 DESCRIPTOR3
ID_55555 (tab) some irrelevant data (tab) DESCRIPTOR200 **DESCRIPTOR1** DESCRIPTOR599
代码:
for line in file:
line = line.strip()
line = line.split("\t")
IDNUMBER = line[0]
DESCRIPTOR = line[2]
for x in total_list:
if x in DESCRIPTOR:
print x, DESCRIPTOR
having a problem here so hopefully could use some help.
I have a text file with an ID number and a set of "descriptors" on each line. The descriptors may or may not be unique to each line (they can be used multiple times throughout the document).
I basically want to identify all the ID numbers that contain a certain descriptor... my code is working but it only finds the first occurrence of the descriptor, instead of all of them. Any quick fix?
All the descriptors are in a list already.
Example of the text file:
ID_45555 (tab) some irrelevant data (tab) **DESCRIPTOR1** DESCRIPTOR2 DESCRIPTOR3
ID_55555 (tab) some irrelevant data (tab) DESCRIPTOR200 **DESCRIPTOR1** DESCRIPTOR599
Code:
for line in file:
line = line.strip()
line = line.split("\t")
IDNUMBER = line[0]
DESCRIPTOR = line[2]
for x in total_list:
if x in DESCRIPTOR:
print x, DESCRIPTOR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议为此使用字典,将描述符作为键,将相应的 ID 作为值。您浏览该文件,并在每一行将 ID 添加到每个描述符下的字典中归档的列表中。例如:
I'd suggest using a dict for this, with the descriptors as the keys and the corresponding IDs as the values. You go through the file and at each line, add the ID to the list filed in the dictionary under each descriptor. For example: