0

When writing to my CSV my list item gets broken up into individual strings even though I am using the append function. I think the problem is in how I am using writerow, but I'm not quite sure.

This is my code.

twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

input_path = 'doc_list.csv'
output_path = 'doc_tweets.csv'

docs_array = []

with open(input_path, 'r') as doc_file:
    docs = csv.reader(doc_file)
    for row in docs:
        docs_array.append(row)

with open(output_path, 'w') as tweets_file:
    writer = csv.writer(tweets_file, quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
    for name in docs_array:
        tweet_list = []
        user_timeline = twitter.get_user_timeline(screen_name=name, count=100, include_retweets=False)
        for tweet in user_timeline:
            tweet_list.append(tweet['text'])
        for li in tweet_list:
            writer.writerow(li)
        del tweet_list[:]

del docs_array[:]

An example of what is in the list before it's iterated for writerow is

, '@etritabaugh gorgeous',

After being passed through writer.writerow(li) it becomes

@,e,t,r,i,t,a,b,a,u,g,h, ,g,o,r,g,e,o,u,s

Artem Yevtushenko
  • 704
  • 1
  • 9
  • 21
  • Thanks @ozgur, I tried adding it into the writerow as an argument, but was returned this error. writer.writerow(li, delimiter=",") TypeError: writerow() takes no keyword arguments Also tried putting it as a csv.writer argument, but got individual strings as before. – Artem Yevtushenko Oct 03 '16 at 14:24

1 Answers1

0

writerow expects an iterable, and once it gets a string, each character in the string gets splitted as separate columns.

To write the entire list as a single row in to the csv, you won't need the second for loop:

for tweet in user_timeline:
     tweet_list.append(tweet['text'])
writer.writerow(tweet_list)

If instead, you want to write each item in the list as a separate row, then use a list literal which will prevent the string from being used as the row:

for tweet in user_timeline:
     tweet_list.append(tweet['text'])
for li in tweet_list:
     writer.writerow([li])
#                    ^  ^
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139