Artem Yevtushenko Artem Yevtushenko - 2 months ago 6
Python Question

how do I prevent my list items from being broken up into individual strings when writing to CSV?

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

Answer

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])
#                    ^  ^