csv python:从csv中的行生成元组列表

时间:2018-06-07 03:00:25

标签: python python-3.x csv tuples

我有数千个具有相同内部格式的csv文件。行通常有八列,格式为:

row1 = ['string in column 0', 'string in column 1', 'string in column 2'...]
row2 = ['string in column 0', 'string in column 1', 'string in column 2'...]
row3 = ['string in column 0', 'string in column 1', 'string in column 2'...]

对于每个文件,我想读取某些行并输出由行分隔的元组列表。例如,文件在前八列中包含字符串,我希望最终输出为:

list_of_tuples = [('row1_col0','row1_col2','row1_col5','row1_col6'),('row2_col0','row2_col2','row2_col5','row2_col6'),...]

并非所有行都包含所有四个值,因此我害怕生成四个列表,然后在读入后构建一个元组。

到目前为止我构建的函数读取

import csv

def list_of_tuples_from_csv(filepath_to_csv):
    total_text = []
    with open(file path_to_csv, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            total_text.append(tuple(row[0],row[2],row[5],row[6]))
    total_posts = list(set(total_text))
    return total_posts

但我读错了

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in list_of_tuples_from_csv
TypeError: tuple() takes at most 1 argument (4 given)

1 个答案:

答案 0 :(得分:1)

您不需要调用import threading def printit(): threading.Timer(120.0, printit).start() print "Hello, World!" t = threading.Timer(120.0, printit) t.start() 函数,只需将其放在括号中即可。

所以这一行

tuple

变成这个

total_text.append(tuple(row[0],row[2],row[5],row[6]))