将文本文件中单独标题下的列保存到单独列表

2024-05-17 11:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下文本文件:

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实现这一点


Tags: idfilevalues文本文件a15fileafilebc15
3条回答

可以使用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')

这是一个典型的例子,在这里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

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

文件内容为:

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

相关问题 更多 >