Skip to content Skip to sidebar Skip to footer

Python Save File To Csv

I have the following code that gets in Twitter tweets and should process the data and after that save into a new file. This is the code: #import regex import re #start process_twe

Solution 1:

With a example file like this:

tweet number one
tweet number two
tweet number three

This code:

file = open('tweets.txt')
for line in file:
   print line

Produces this output:

tweet number one

tweet number two

tweet number three

Python is reading in the endlines just fine, but your script is replacing them via regular expression substitution.

this regex substitution:

tweet = re.sub('[\s]+', ' ', tweet)

Is converting all of your white space characters (e.g tabs and new lines) into single spaces.

Either add a endline onto the tweet before you output it, or modify your regex to not substitute endlines like so:

tweet = re.sub('[ ]+', ' ', tweet)

EDIT: I put my test substitution command in there. the suggestion has been fixed.

Post a Comment for "Python Save File To Csv"