2017-05-29 25 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)