尝试访问时出现CSV文件错误

时间:2017-02-16 10:51:53

标签: python csv

情形: 我正试图从twitter中提取推文,这工作正常, 接下来我试图将10个文件合并为1(比如file = QW)。

    for line in file: 
      my_row = [line]
      filename = line.rstrip()+"_tweets"+".csv"
      if(os.path.exists(filename)):
        f = open(filename, "rt")
        reader = csv.reader(f, delimiter="\t")
        for line in enumerate(reader):
            my_row.append(line) 
        writer.writerow(my_row) 
    else:
        print(""+filename+ " doesnt exist")

my csv file looks like this 然后我将处理那个文件

问题:我想阅读该CSV(QW)文件的特定列

我试过了行[0]

    for row in input_file:
    name_list = [] ;score = 0;
    name_list.append(row[0])
    print(name_list)
    for a in row:
        if a.find(skill_input) > 0 :
            score = score+1;


    name_list.append(score)
    print(name_list)
    writer.writerow([name_list])

那一点我得到一个错误 my csv file looks like this        name_list.append(行[0])        IndexError:列表索引超出范围

1 个答案:

答案 0 :(得分:0)

试试这个

for line in open("csvfile.csv"):
    csv_row = line.split(your_delimiter) #returns a list ["1","50","60"]
    if not csv_row[k]:
        continue
    name_list.append(csv_row[k]) # Or csv_row[k] for specific kth column