我有一个像
这样的实验数据的大文本文件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)即可。如果它们有多个值,则打印下一行。
答案 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)