python - 索引错误: list index out of range CSV parser

标签 python csv parsing

我正在尝试使用此代码来解析 csv 文件,但无法找到解决此错误的方法:

"file"(文件位置)”,第 438 行,parser_42

位置 = tmp2[1]

索引错误:列表索引超出范围”

我的 csv 文件的结构如下:

突变系数得分

Q41V -0.19 0.05

Q41L -0.08 0.26

Q41T -0.21 0.43

I23V -0.02 0.45

I61V 0.01 1.12

例如,我想采用突变体并将“Q”“41”和“V”分开。 然后我想创建位置和重量列表并将它们按数字顺序排列。

目标是将字符串“seq”写入新的 csv 文件

显然,我是Python和数据操作的初学者。我想我只是忽略了一些愚蠢的事情......任何人都可以引导我走向正确的方向吗?

def parser_42(csv_in, fasta_in, *args):

    with open(csv_in, 'r') as tsv_in:
        tsv_in = csv.reader(tsv_in, delimiter='\t')
        next(tsv_in) # data starts on line 7
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)

        for row in tsv_in:
            tmp = row[0].split(',')
            tmp2 = re.split('(\d+)', tmp[0])
            wt = tmp2[0]
            position = tmp2[1]
            substitution = tmp[2]

            seq = ""
            current_positions = []


            if position not in current_positions:
                current_positions += [position]
                print(current_positions)
                seq += wt
            else:
                continue

        print(seq)

最佳答案

对于任何可能感兴趣的人,这就是我解决问题的方法...如果有人对如何使其更简洁有任何建议,我们将不胜感激。我知道这可能看起来像是解决小问题的一种迂回方式,但我在这个过程中学到了很多东西,所以我并不过分担心:)。我基本上用正则表达式替换了.split(),这似乎更干净一点。

def parser_42(csv_in, fasta_in, *args):
    dataset = pd.DataFrame(columns=get_column_names())
    with open(csv_in) as tsv_in:
        tsv_in = csv.reader(tsv_in, delimiter='\t')
        next(tsv_in) #data starts on row 7
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)
        next(tsv_in)
        save_path = '(directory path)'
        complete_fasta_filename = os.path.join(save_path, 'dataset_42_seq.fasta.txt')
        output_fasta_file = open(complete_fasta_filename, 'w')

        seq = ''
        current_positions = []

        for row in tsv_in:

         # regular expressions to split numbers and characters in single cell
            regepx_match = re.match(r'([A-Z])([0-9]+)([A-Z,*])', row[0], re.M | re.I)
            wt = regepx_match.group(1)
            position = int(regepx_match.group(2))
            substitution = regepx_match.group(3)

            if position not in current_positions:
                current_positions += [position]
                seq += wt
            else:
                continue
        seq = list(seq)

    # this zips seq and current_positions and sorts seq
        sorted_y_idx_list = sorted(range(len(current_positions)), key=lambda x: current_positions[x])
        Xs = [seq[i] for i in sorted_y_idx_list]

        seq1 = '>dataset_42 fasta\n'
        seq1 = seq1 + ''.join(Xs) # join to string


        output_fasta_file.write(seq1)
        output_fasta_file.close()

关于python - 索引错误: list index out of range CSV parser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333263/

相关文章:

MySQL:使用 LOAD DATA 时将数据压缩在一列中

ruby - 如何从字符串执行日期?

java - 在 php 中。显示一个长字符串作为java代码显示

python - pandas - 使用 for 循环将多列附加到数据框

python - 如何在 python 中成对修改列表中的值?

python - 如何将参数列表转换为 Python 中的单个字符串命令行?

r - 数据表读取功能

python - 如何从变量中获取数据并将其放入另一个变量中

python - 从 CSV 中提取信息

parsing - 忽略 Antlr4 中的空格(在某些部分)