[英]Add a column to a CSV using Python
I have a CSV file, which looks like this: 我有一个CSV文件,看起来像这样:
id,text,initial_score
1,"Today's news: Democrats offer Republicans everything they asked for; Republicans demand more. Not hard to understand: R's want a shutdown.",0
I want to create a new file, which include the same fields and add a new field as a column. 我想创建一个包含相同字段的新文件,并将新字段添加为一列。 The new field will be a result of an equalization.I used the following code but there is a syntax error:
新字段将是均衡的结果。我使用了以下代码,但是存在语法错误:
f1 = open(filepathIntro)
f = open(filepath)
for line in f1:
cols = split_line(line)
words1 = get_tweet_words(cols)
total_score = 0
for w1 in words1:
for line in f:
if not line.startswith("#"):
cols = split_line(line)
words2 = get_words(cols)
for w2 in words2:
if w1 == w2:
posnum = float(get_positive(cols))
negnum = float(get_negative(cols))
total_score = total_score + (posnum - negnum)
with open(filepathIntro, 'r') as f1, open('semevalSenti.csv', 'w+' ) as fout:
reader = csv.reader(f1)
writer = csv.writer(fout)
writer.writerow(next(reader) + ['Total score'])
writer.writerows([reader] + float(total_score) )
The message error is: 消息错误是:
writer.writerows([a] + total_score for a,total_score in zip(reader,total_score)) TypeError: zip argument #2 must support iteration
Could you please help me? 请你帮助我好吗? Thanks in advance!!!!!!
提前致谢!!!!!!
You are missing a closing paren at writer.writerow(next(reader) + ['Total score'] <-
, also remove the set literal just zip(reader, total_score)
您缺少
writer.writerow(next(reader) + ['Total score'] <-
的结束符,还删除了zip(reader, total_score)
I presume writer.writerow(line + total_score)
should be writer.writerow(line + [val])
which means you can simplify the process by using writerows
using the result of zipping once total_score
is defined and an iterable the same length as the number of rows in the file originally: 我假设
writer.writerow(line + total_score)
应该是writer.writerow(line + [val])
,这意味着一旦定义了total_score
并且可迭代的长度与数字相同,就可以通过使用压缩结果使用writerows
来简化过程最初在文件中的行数:
with open(filepathIntro, newline='') as f1, open('semevalSenti.csv',newline='', 'w') as fout:
reader = csv.reader(f1)
writer = csv.writer(fout)
writer.writerow(next(reader) + ['Total score'])
writer.writerows(a + [b] for a,b in zip(reader, total_score)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.