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

标签 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 来做到这一点?

最佳答案

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

关于python - 将文本文件中单独标题下的列保存到单独的列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29315292/

相关文章:

regex - Sed 替换文本

python - 如何检查浮点值是否为整数

python - flask子函数未产生结果

Python - 在文本文件中搜索字符串

php - 每 x 行分割文本文件

c# - 将文本写入文件或转换为字节并将其写入文件哪个更快?

python - 使用 setup.py 构建 Python native 扩展时应用 C 编译器 CFLAGS

python - matplotlib:获取投影坐标

html - 图片在文本旁边对齐

android - 使用 bulletspan 时换行不正确