Ibrahim Dangou
Ibrahim Dangou

Reputation: 19

how to stop writer.writerows(all) from repeating the lines after finishing it

I use this code to read tweets from CSV file and write the result in new file. it is working well but the problem is that the writer does not stop and repeat the line again and again please any help?

with open('D:\oopp.csv', 'rU') as csvinput:
    with open('D:/test8_datasets_output.csv', 'wb') as csvoutput:
        writer = csv.writer(csvoutput,lineterminator='\n')
        reader = csv.reader(csvinput)

        all=[]
        row = next(reader)

        for row in reader:
            features = get_features_from_tweet(row[0])
            result1 = classifier1.classify(features)
            result2 = classifier2.classify(features)

            row.append(result1)
            row.append(result2)

            all.append(row)

            writer.writerows(all)

OUT PUT: [["Tesla years away from its goal of reaching Apple's market cap http://t.co/uv6q87moTt", 'pro', 'neg']] [["Tesla years away from its goal of reaching Apple's market cap http://t.co/uv6q87moTt", 'pro', 'neg'], ['Apple $1 trillion stock market value could be years away http://t.co/twZmljm5y3', 'pro', 'neg']] [["Tesla years away from its goal of reaching Apple's market cap http://t.co/uv6q87moTt", 'pro', 'neg'], ['Apple $1 trillion stock market value could be years away http://t.co/twZmljm5y3', 'pro', 'neg'], ['Apple has banned "bonded servitude" at supplier factories worldwide http://t.co/kcJGLtgMjF http://t.co/KCYwDyqBEU', 'in', 'po']] [["Tesla years away from its goal of reaching Apple's market cap http://t.co/uv6q87moTt", 'pro', 'neg'], ['Apple $1 trillion stock market value could be years away http://t.co/twZmljm5y3', 'pro', 'neg'], ['Apple has banned "bonded servitude" at supplier factories worldwide http://t.co/kcJGLtgMjF http://t.co/KCYwDyqBEU', 'in', 'po'], ["Alibaba's Jack Ma seeks to reassure employees over U.S. lawsuits http://t.co/ykFribORTH", 'in', 'po']]

Upvotes: 0

Views: 359

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123860

You are appending to a list then writing that list inside the loop.

Rather than append to a list, just write each row separately with writer.writerow() (no s):

with open('D:\oopp.csv', 'rU') as csvinput:
    with open('D:/test8_datasets_output.csv', 'wb') as csvoutput:
        writer = csv.writer(csvoutput,lineterminator='\n')
        reader = csv.reader(csvinput)

        row = next(reader)

        for row in reader:
            features = get_features_from_tweet(row[0])
            result1 = classifier1.classify(features)
            result2 = classifier2.classify(features)
            writer.writerow(row + [result1, result2])

Upvotes: 4

Related Questions