0

I have a CSV file, which looks like this:

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!!!!!!

1
  • 1
    Could you please provide the error message you got? Commented Nov 25, 2015 at 19:52

1 Answer 1

1

You are missing a closing paren at writer.writerow(next(reader) + ['Total score'] <-, also remove the set literal just 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:

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)
Sign up to request clarification or add additional context in comments.

19 Comments

now shows me this error: reader = csv.reader(f1, newline='', lineterminator='\n') TypeError: 'newline' is an invalid keyword argument for this function
@NellyBrili, I removed all the keywords, newline="" would be used with open not csv.writer, the code as is should work fine
It's almost done! but now throws me the following error: writer.writerows([a] + b for a,b in zip(reader, total_score)) TypeError: zip argument #2 must support iteration
total score is the result of an equalization. Each line have words, each word have a sentiment, so total score is the sum of that sentiment. total_score=total_score+(pos_sentiment - neg_sentiment)
So a calculation based on existing values from each row? If so you will need to replace the [b] in the code with [your_calculation]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.