使用 Python 向 CSV 添加一列
Add a column to a CSV using Python
我有一个 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
我想创建一个新文件,其中包含相同的字段并添加一个新字段作为列。新字段将是 equalization.I 使用以下代码但存在语法错误的结果:
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) )
消息错误为:
writer.writerows([a] + total_score for a,total_score in zip(reader,total_score)) TypeError: zip argument #2 must support iteration
你能帮帮我吗?提前致谢!!!!!!
您在 writer.writerow(next(reader) + ['Total score'] <-
处缺少右括号,还删除了仅 zip(reader, total_score)
的集合文字
我认为 writer.writerow(line + total_score)
应该是 writer.writerow(line + [val])
这意味着您可以通过使用 writerows
来简化过程,一旦定义了 total_score
和可迭代的与文件中原来的行数相同的长度:
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)
我有一个 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
我想创建一个新文件,其中包含相同的字段并添加一个新字段作为列。新字段将是 equalization.I 使用以下代码但存在语法错误的结果:
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) )
消息错误为:
writer.writerows([a] + total_score for a,total_score in zip(reader,total_score)) TypeError: zip argument #2 must support iteration
你能帮帮我吗?提前致谢!!!!!!
您在 writer.writerow(next(reader) + ['Total score'] <-
处缺少右括号,还删除了仅 zip(reader, total_score)
我认为 writer.writerow(line + total_score)
应该是 writer.writerow(line + [val])
这意味着您可以通过使用 writerows
来简化过程,一旦定义了 total_score
和可迭代的与文件中原来的行数相同的长度:
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)