将列保存在文本文件中的单独标题下以分隔列表

时间:2015-03-28 08:50:37

标签: python text

我有以下文字文件:

File A
am001 G13 
am002 U13
am003 A15

File B
am001 C15
am002 U2715
am003 G32   

我想将单独标题下的列值保存到单独的列表中。例如,输出应如下所示:

filea_id = ['am001', 'am002', 'am003']
filea_values = ['G13', 'U13', 'A15']
fileb_id = ['am001', 'am002', 'am003']
fileb_values = ['C15', 'U2715', 'G32']

如何使用python执行此操作?

3 个答案:

答案 0 :(得分:1)

您可以使用collections.deque存储特定行,然后使用splitzip来获得预期结果:

from collections import deque
d=deque(maxlen=3)
with open('output.txt') as f:
    for line in f :
        if line != '\n' and 'File' not in line :
              d.append(line)
        if len(d)==3:
             filea_id ,filea_values =zip(*[i.split() for i in d])
             print filea_id ,filea_values
             d.clear()
             #or do stuff with filea_id ,filea_values

结果:

('am001', 'am002', 'am003') ('G13', 'U13', 'A15')
('am001', 'am002', 'am003') ('C15', 'U2715', 'G32')

答案 1 :(得分:1)

首先,您需要阅读并解析源文件。如果您的源文件只有两列,则代码非常简单。

文件的内容是:

am001 G13 
am002 U13
am003 A15

要解析它,请执行:

data = {}

with open("src.txt") as fp:
    for line in fp:
        key, value = line.split()
        data[key] = value

print data

这将产生字典:

{'am003': 'A15', 'am002': 'U13', 'am001': 'G13'}

现在你可以遍历字典,并构建自己的格式,以便查看或写入另一个文件:

for key, value in data.iteritems():
    print key, value

答案 2 :(得分:1)

这是一个典型的例子,其中itertools满足其期望。

<强>实施

def foo():
    from itertools import izip, imap, takewhile
    with open("temp.txt") as fin:
        def skip(fin):
            # Take/Read all lines from the file and discard which are empty
            takewhile(lambda name: name.strip() == "", fin)
        try:
            while fin:
                # Skip all empty lines
                skip(fin)
                # The next line is the file name
                fname = next(fin).strip()
                # All subsequent lines until the empty line is the content
                # Split the lines and transpose it 
                # Yield the file name and the transposed content
                yield fname, zip(*(imap(str.split, takewhile(lambda n:n.strip(), fin))))
        except StopIteration:
            pass

<强>演示

>>> content ={}
>>> for fname, data in foo():
    content[fname]=data


>>> content
{'File A': [('am001', 'am002', 'am003'), ('G13', 'U13', 'A15')], 'File B': [('am001', 'am002', 'am003'), ('C15', 'U2715', 'G32')]}

<强>解释

[Skip All Empty Lines]                                  [Split each line]     [Transpose]      
    V        
    V        
[The Next Line is the File Name]  fname =  File A                                 
[Read Until an empty line]                 am001 G13       am001 | G13      am001 am002 am003
    V                                      am002 U13 >>>   am002 | U13  >>> G13   U13   A15
    V                                      am003 A15       am003 | A15
[Skip All Empty Lines]
    V
    V    
[The Next Line is the File Name]  fname =  File B           
[Read Until an empty line]                 am001 C15       am001 | C15        am001 am002 am003
    V                                      am002 U2715 >>> am002 | U2715  >>> C15   U2715 G32
    V                                      am003 G32       am003 | G32