2017-05-29 26 views
0

由行文本文件变换线到制表符分隔的格式通过检查报头我有实验数据的像通过蟒

spectrum: 
index: 1 
mz: 4544.5445 
intensity: 57875100000 
type: 1 
something: skip 
params - m1 
binary: [4] 1 2 3 4 
params - int1 
binary: [4] 11 22 33 44 
spectrum: 
index: 2 
mz: 546.7777 
intensity: 210009 
type: 2 
params - m2 
binary: [4] 2 3 4 5 
params - int2 
binary: [4] 55 44 33 22 
charge: 3 
others: no need to put into column 
spectrum: 
index: 3 

欲打印出来以csv文件,信息中的每个频谱数据的大的文本文件放在与其标题相同的行中。如果他们没有该信息头中的信息,只需跳过(或放入NA)即可。如果它们有多个值,则打印每一行。

python是否有一些简单的方法来获得这样的结果? enter image description here

+0

当然,就去做。一次读取一条记录,随时将每条记录写入一个csv文件。你没有发布任何代码表明问题,所以看起来你没有付出任何努力,并没有陷入困境。所以就做吧。 –

+0

您的列名不一致排列并且有重复。 –

回答

0

你想这样的事情:

PSEYDOCODE

class Spectrum(): 
    def add(self, text): 
     column, value = text.split(' ') 
     if column == 'index:' 
      self._csv['index'] = int(value) 
     elif column == 'mz:' 
      self._csv['mz'] = float(value) 
     ... an so on 

spectrum = Spectrum() 
with text file as in_file 
    for line in in_file 
    if line == 'spectrum:' 
     if in_spectrum 
      spectrum.expand_to_csv() 
      spectrum = Spectrum() 
     in_spectrum = True 
     continue 

    spectrum.add(line)