【问题标题】:Extracting only interesting columns from ASCII table从 ASCII 表中仅提取感兴趣的列
【发布时间】:2012-07-13 01:48:08
【问题描述】:

我绝不是程序员,但我偶然发现了一个非常讨厌的固定宽度 ASCII 表,这可能需要我成为一个 :)(我希望你们能提供一些帮助)

我确实已经向 Google 先生征求了一些建议,他为我指出了 Python 的方向。所以我在这里 - 非常迷茫:(

有问题的表如下所示:

column1 column2 column3 column4 column5 column6 column7 ... columnN
   数据 垃圾 垃圾 数据 垃圾 垃圾 数据
   数据 垃圾 垃圾 数据 垃圾 垃圾 数据
   数据垃圾垃圾数据垃圾垃圾
   数据垃圾垃圾垃圾垃圾
   数据 垃圾 垃圾 数据 垃圾 垃圾 数据
   数据 垃圾 垃圾 数据 垃圾 垃圾 数据
   数据 废话 废话 废话 数据
   数据垃圾数据垃圾数据垃圾数据
   数据 垃圾 垃圾 数据 垃圾 垃圾 数据
   数据垃圾垃圾数据垃圾垃圾数据

如您所见,列数可能会有所不同,表中有些部分没有数据,也有一些列包含我不感兴趣的数据。

我的目标是在最后有一个如下所示的表格:

column1 column4 column7 ... columnN
   数据数据数据
   数据数据数据
   数据数据
   数据
   数据数据数据
   数据数据数据
   数据数据
   数据数据数据
   数据数据数据
   数据数据数据

所以,现在我不想要的所有列都消失了。这基本上是我的目标 - 一个只包含我感兴趣的列的表。你认为这样的事情可以在 Python 中完成吗?

【问题讨论】:

    标签: python tabular


    【解决方案1】:

    是的,这是可以做到的。在 python 中,字符串是序列,因此您可以使用固定索引将行分割成列:

    >>> row = "   data    crap    crap            crap    crap   data"
    >>> width = 8 # Column width
    >>> columns = [row[i*width:(i+1)*width].strip() for i in range((len(row)/width)+1)]
    >>> columns
    ['data', 'crap', 'crap', '', 'crap', 'crap', 'data']
    

    现在您所要做的就是选择您的列:

    >>> columns[0], columns[3], columns[6]
    ('data', '', 'data')
    

    我可以想象上面的代码在你看来仍然是胡言乱语;我强烈建议您开始阅读有关学习编程的内容。 Python 是一门出色的语言,从 http://wiki.python.org/moin/BeginnersGuide 开始,然后一路向上!

    【讨论】:

    • 非常感谢Martijn!我也会试试的。现在我的脑袋被所有这些新的 Python 东西弄得晕头转向 :)
    【解决方案2】:

    听起来您正试图从文本文件中读取表格信息,然后重新格式化它。一些基本处理可能如下所示:

    # First read content into an array
    # Each item in the array will be a line of the file
    with open('filename.txt') as f:
        content = f.readlines()
    
    # Next, parse each line
    data = []
    for line in content:
        # You might need to split by spaces
        # This takes care of multiple whitespaces, so "data1   data2 data3    data4"
        # Becomes ['data1','data2','data3','data4']
        row = line.split()
        # Or, maybe you will need to split the row up by tabs into an array
        # [] is a list comprehension, strip() will remove extra whitespace
        row = [item.strip() for item in line.split('\t')]
        # Finally, append the row to your data array
        data.append(row)
    
    # Now, print the data back to a file how you'd like
    fout = open('output.txt','w')
    for row in data:
       # For specific columns
       fout.write('{0} {1} {2} {3}'.format(row[0],row[1],row[7],row[8]))
       # Or, if you just need to remove a couple columns, you might do:
       row.pop(6)
       row.pop(5)
       row.pop(4)
       fout.write(' '.join(row))
    

    【讨论】:

    • 嗨亚当!我正在使用您的解决方案,我认为这是我开始使用 Python 的起点。非常感谢!
    • 这将从列表中删除空列; "data __________ crap __________ crap crap" 变为 ['data','crap','crap','crap'],中间没有空列(下划线用于说明 cmets 中的空白)。
    • 当然 - 是的,如果您不想使用 split() 删除随机的空列,请使用 Martijn Pieters 的酷炫解决方案根据每列的固定字符宽度保留这些。跨度>
    【解决方案3】:

    虽然我真的认为你应该为了学习 python 而在 python 中编写代码,但如果你只想完成它,请尝试使用 Excel!

    • 阅读桌子中(如果Excel无法弄清楚这一点,我会感到惊讶!)
    • 删除您对
    • 不感兴趣的列
    • 导出/另存为固定宽度

    【讨论】:

    • Excel的“tool to列”功能(在Excel 2007中的数据选项卡下)将很好地处理。您可以在Python作为学习体验,但您只需要完成 i>,请使用Excel。 span>
    • 谢谢达伦,但文件有点大于Excel。也许我应该提到这一点 - 它是一个巨大的文件。顺便说一句,您的个人资料照片看起来很吓人:)
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2015-06-07
    • 2017-09-28
    • 2021-04-05
    • 2011-08-10
    相关资源
    最近更新 更多