【问题标题】:grabbing info from a table with loops (python)从带有循环的表中获取信息(python)
【发布时间】:2016-09-20 21:08:59
【问题描述】:
  • 我正在尝试分析此文本文件中的一些信息(其中之一 许多我想以相同的格式分析)

  • .txt 文件中有一个表格,其中包含我需要的信息

  • 表格总是有 16 列,但行数会有所不同

  • 该表的列由管道“|”分隔以及由这些分隔的行:'+--------+--------+'

  • 我将文件 (.split('+---+')) 拆分为一个列表 ('newlist'),其中每个 元素是一行(第 1 行 = newlist[0])

  • 我在 表格结束('..image::' 在哪里)
  • 现在我想将行拆分为 他们的专栏我可以用 .split('|') 轻松做到这一点

  • 我创建了一些运行良好的循环并考虑了一个变量 行数

  • def row() 将新列表放入 list_i list_i 是 列表,其中每个元素是该行中一个框的内容(使用 split('|') 对于这个特定的测试文件,我可以上到第 (29) 行

    • 我对按列的数据感兴趣 下一个循环创建一个列表 使用列信息 def column() 查看范围内的所有行(数字 行数)并为所有这些行提取相同的索引。所以栏(9) 会拉row(0)[9], row(1)[9]....一直拉到最后一行

    • 我的问题是,这在我到达第 (9) 列之前效果很好,然后 它说列表索引超出范围

  • 对不起,我知道这个问题已经被问过很多次了,但不知道怎么回事

    谢谢!

输入文件:https://drive.google.com/open?id=0B_JDBrcvs5VcRU1ueE5kUlVoYlk

    f = open("999A.txt")


    text_in_file = f.read().strip().split('+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+')
    f.close()

    newlist = []

    for item in text_in_file:
        newlist.append(item)
    matching = [s for s in newlist if ".. image::" in s]

    for item in newlist:
        if newlist.index(item) >= newlist.index(matching[0]):
            newlist.remove(item)


    num_rows = len(newlist) - 1


    def row(i):
        row_i = newlist[i+1]
        list_i = list(row_i.strip().split('|'))
        return list_i[1:17]

    def column(i):
        list_i = []
        for z in range(num_rows):
            list_i.append(row(z)[i])
        return list_i[1:]

    for i in range(30):
        print(row(i))
    print("columns:")
    for i in range(16):
        print(column(i))

【问题讨论】:

  • 请出示您的输入文件;或者更好的是,显示产生相同问题的输入文件的小版本。
  • 我会忽略所有不以| 开头的行,并拆分那些在| 上执行的行...这似乎是您获取列所需的全部内容跨度>

标签: python


【解决方案1】:

表格总是有 16 列

不正确,您只有 8 个标题,因此您将在该行上遇到索引错误。

|  *L1 barcodes*  |  *L2 barcodes*  |  *L3 barcodes*  |  *L4 barcodes*  |  *L5 barcodes*  |  *L6 barcodes*  |  *L7 barcodes*  |  *L8 barcodes*  |
| CTCTCT | 27.66% | GTTTCG | 9.04%  | NNNNNN | 3.67%  | ATTCGG | 7.41%  | GACGAT | 6.90%  | GAACCC | 13.29% | GTAACA | 9.50%  | ATCGCC | 56.24% |

查看示例代码

with open("999A.txt") as f:
    for line in f:
        line = line.strip()
        if line.startswith("|"):
            print line

如果您只想获取具有所需列数的行,那么您需要像这样检查拆分行的长度。

data = []
with open("999A.txt") as f:
    for line in f:
        line = line.strip()
        if line.startswith("|"):
            cols = line.split("|")[1:-1] # remove outside empty strings
            cols = list(map(str.strip, cols)) # strip the remaining strings
            if len(cols) == 16 and not all(x == '' for x in cols):
                # keep rows with 16 columns and no empty strings
                data.append(cols)

for row in data:
    # do something
    print(row)

样本输出

['CTCTCT', '27.66%', 'GTTTCG', '9.04%', 'NNNNNN', '3.67%', 'ATTCGG', '7.41%', 'GACGAT', '6.90%', 'GAACCC', '13.29%', 'GTAACA', '9.50%', 'ATCGCC', '56.24%']
['TGTGTG', '27.54%', 'ATTCCT', '5.78%', 'TTCAGA', '3.11%', 'CGAATC', '6.70%', 'ATTCGG', '6.45%', 'TGCTGT', '13.18%', 'TGCTGT', '8.64%', 'GCTATT', '9.98%']
['ACACAC', '22.70%', 'ATGTCA', '4.47%', 'AGGTTT', '3.01%', 'GACGAT', '6.36%', 'CCATTA', '6.37%', 'TTCAGA', '12.19%', 'CCTGAG', '7.82%', 'CCGAGT', '8.79%']
['GAGAGA', '16.18%', 'GTGGCC', '4.06%', 'CCTGAG', '2.71%', 'GCTATT', '6.26%', 'TTGCCG', '6.23%', 'CCTGAG', '11.42%', 'AAGCTC', '7.77%', 'TAATAG', '5.72%']
['', '', 'GNNTNG', '3.96%', 'GAACCC', '2.47%', 'AGTAGC', '6.11%', 'TAGGCT', '6.14%', 'AGGTTT', '11.39%', 'GAACCC', '7.62%', 'CCATTA', '3.70%']
['', '', 'GTGAAA', '3.47%', '', '', 'CCATTA', '6.10%', 'GCCTAA', '6.07%', 'GTAACA', '11.36%', 'CTTAAA', '7.56%', '', '']

您可能还希望将列表中的每对元素分组以保留最初的 8 列

应该是这样的

...
# keep rows with 16 columns and no empty strings
cols_iter = iter(cols)
data.append(list(zip(cols_iter, cols_iter)))

这样的输出

[('CTCTCT', '27.66%'), ('GTTTCG', '9.04%'), ('NNNNNN', '3.67%'), ('ATTCGG', '7.41%'), ('GACGAT', '6.90%'), ('GAACCC', '13.29%'), ('GTAACA', '9.50%'), ('ATCGCC', '56.24%')]
[('TGTGTG', '27.54%'), ('ATTCCT', '5.78%'), ('TTCAGA', '3.11%'), ('CGAATC', '6.70%'), ('ATTCGG', '6.45%'), ('TGCTGT', '13.18%'), ('TGCTGT', '8.64%'), ('GCTATT', '9.98%')]

在此基础上,您可以打印每个元素

for row in data:
    # do something
    for seq, percent in row:
        if not '' in {seq, percent}:
            print(seq, percent)

输出

CTCTCT 27.66%
GTTTCG 9.04%
NNNNNN 3.67%
ATTCGG 7.41%
GACGAT 6.90%

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 2022-07-07
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多