拆分一个字符串并将子字符串保存到dict。蟒蛇

时间:2014-02-20 17:17:53

标签: python regex string dictionary

我有一个这样的文本文件:

771 776 #1 556.766700(2)
538 #2 1069.652700(2)
531 #3 1074.407600(2)
81 84 89 94 111 #4 1501.062900(2)
85 91 #5 782.298900(3)
32 42 66 71 90 95 101 #6 904.016500(3)

我想将子串分割并保存到不同的变量,如下所示: 例如第1行:

scans= 771 776, uid = 1 mz = 556.766700, z = 2

我正在尝试使用以下代码,但我需要有关正则表达式的帮助:

f = open(filename, 'r')
par_info=[]
for rows in f:
    re.sub('\#(.+)\s(.+)\((.+)\+', scans=\g<1>, uid=\g<2>, mz = int(\g<3>),    z=int(\g<4>), rest)
    info={'sc_num':scans, 'ident':uid, 'mass':mz, 'charge':z}
    par_info.append(info)

4 个答案:

答案 0 :(得分:5)

您可以使用命名组:

>>> import pprint
>>> import re
>>> r = re.compile(r'(?P<scans>.*?)\s+#(?P<uid>\d+)\s+(?P<mz>\d+\.\d+)\((?P<z>\d+)\)')
>>> with open('abc1') as f:
        par_info = [r.search(line).groupdict() for line in f]
...     
>>> pprint.pprint(par_info)
[{'mz': '556.766700', 'scans': '771 776', 'uid': '1', 'z': '2'},
 {'mz': '1069.652700', 'scans': '538', 'uid': '2', 'z': '2'},
 {'mz': '1074.407600', 'scans': '531', 'uid': '3', 'z': '2'},
 {'mz': '1501.062900', 'scans': '81 84 89 94 111', 'uid': '4', 'z': '2'},
 {'mz': '782.298900', 'scans': '85 91', 'uid': '5', 'z': '3'},
 {'mz': '904.016500', 'scans': '32 42 66 71 90 95 101', 'uid': '6', 'z': '3'}]

答案 1 :(得分:1)

import re
pattern = re.compile("(\d+\s*\d*)\s+#(\d+)\s+([\d\.]+)\s*\((\d+)\)")
for line in open("Input.txt"):
    scans, uid, mz, z = pattern.findall(line)[0]
    print scans, uid, mz, z

<强>输出

771 776 1 556.766700 2
538 2 1069.652700 2
531 3 1074.407600 2
94 111 4 1501.062900 2
85 91 5 782.298900 3
95 101 6 904.016500 3

RegEx演示

Regular expression visualization

Debuggex Demo

答案 2 :(得分:0)

如果您的数据总是和示例

一样,我会使用split
par_info = []
with open(filename, 'r') as f:
    for line in f:
        scan, other = line.split("#")
        uid, more = other.split()
        mz, z = other.split('(')
        z = z.replace(')','')
        info = {'sc_num': scans, 'ident': uid, 'mass': mz, 'charge': z}
        par_info.append(info)

答案 3 :(得分:0)

这个正则表达式有效,然后你可以将找到的组拉链在一起并将它们变成一个字典:

In [1]: import re

In [2]: a = "771 776 #1 556.766700(2)"

In [3]: c = re.compile(r'([\d\s]+)\s#(\d)+\s([\d\.]+)\((\d+)\)')

In [4]: titles = ('sc_num', 'ident', 'mass', 'charge')

In [5]: dict(zip(titles, c.search(a).groups()))
Out[5]: {'charge': '2', 'ident': '1', 'mass': '556.766700', 'sc_num': '771 776'}

将它与您的代码放在一起就可以得到:

f = open(filename, 'r')
c = re.compile(r'([\d\s]+)\s#(\d)+\s([\d\.]+)\((\d+)\)')
titles = ('sc_num', 'ident', 'mass', 'charge')
par_info=[]
for row in f:
    info = dict(zip(titles, c.search(row).groups()))
    par_info.append(info)