从.txt解析Python字符串 [英] Python string parsing from .txt
问题描述
我有以下形式的字符串:
I have strings of the following form:
}# => 2[1 HMDB00001 ,2 HMDB00002]
}# => 5[1 HMDB00001 ,2 HMDB00002, 3 HMDB00003 ,4 HMDB00004,5 HMDB00005]
}# => 1[1 HMDB00001]
.p文件中的
.我正在尝试使用带有正则表达式的re.search()在python列表中解析它们,但到目前为止没有成功.如您所料,列表中应包含以下elements = ["1 HMDB00001", "2 HMDB00002", "3 HMDB00003"]
元素.列表彼此独立.因此,解析时只能考虑(eg. }# => 2[1 HMDB00001 ,2 HMDB00002])
.
in a .txt file. I am trying to parse them in python lists using the re.search() with regular expressions, but so far unsuccessful. As u can guess the list should contain elements as follows elements = ["1 HMDB00001", "2 HMDB00002", "3 HMDB00003"]
. Lists are independent from each other. So, when parsing only one line can be taken in consideration (eg. }# => 2[1 HMDB00001 ,2 HMDB00002])
.
推荐答案
这似乎有效,但是鉴于您的问题很难确定.您也许可以根据得到的答案拼凑出解决方案.
This seems to work, but its hard to tell for sure given your question. You may be able to piece together a solution from the answers you get.
import re
strings = [
'}# => 2[1 HMDB00001 ,2 HMDB00002]',
'}# => 5[1 HMDB00001 ,2 HMDB00002, 3 HMDB00003 ,4 HMDB00004,5 HMDB00005]',
'}# => 1[1 HMDB00001]',
]
for s in strings:
mat = re.search(r'\[(.*)\]', s)
elements = map(str.strip, mat.group(1).split(','))
print elements
哪个输出:
['1 HMDB00001', '2 HMDB00002']
['1 HMDB00001', '2 HMDB00002', '3 HMDB00003', '4 HMDB00004', '5 HMDB00005']
['1 HMDB00001']
这篇关于从.txt解析Python字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!