a=['Business', 'Food/Clothes', 'Fun', 'Politics', 'Starting_with_Apolog', ['NNP', 'MD', 'NN', 'NNP'], ['NNP', 'NN', 'NNP'], ['PDT', 'MD', 'NN', 'NNP'], ['PRP$', 'MD', 'NN', 'NNP'], ['UH', 'MD', 'NN', 'NNP'], ['WP$', 'MD', 'NN', 'NNP'], 'end__with_ly', 'end_with_al', 'end_with_ful', 'end_with_ible', 'end_with_ic', 'end_with_ive', 'end_with_less', 'end_with_ous', 'sorry_word', 'Gender']
f = open("file.csv")
reader = csv.reader(f)
headers = None
results = []
for row in reader:
if not headers:
headers = []
for i, col in enumerate(row):
if col in a:
# Store the index of the cols of interest
headers.append(i)
print headers
else:
results.append(list([row[i] for i in headers]))
return results
上面的代码是一个从FILE.CSV在列表读取特定列,以便其结果将是在结果可用,但索引代码只会索引以下的列:如何从CSV读取列
** Fun 63
** Food/Clothes 64
** Politics 70
** Business 73
** end_with_al 75
** end_with_ful 76
** end_with_ible 77
** end_with_ic 78
** end_with_ive 79
** end_with_less 80
** end__with_ly 81
** end_with_ous 82
** sorry_word 83
** Starting_with_Apolog 84
** Gender 1487
该代码不索引列表中的列表 - 我怎样才能让代码搜索它们呢? 注意:file.csv包含一些带有1487列的数据; a包含来自file.csv的一些列。
你确定你使用细分离? – BenjaminB
是的......当然...... – user104853
“a”中的子列表有什么意义? – ForeverWintr