Python将文件保存到csv

Python将文件保存到csv,python,csv,Python,Csv,我在Twitter推文中有以下代码,应该处理数据,然后保存到一个新文件中 代码如下: #import regex import re #start process_tweet def processTweet(tweet): # process the tweets #Convert to lower case tweet = tweet.lower() #Convert www.* or https?://* to URL tweet = re.su

我在Twitter推文中有以下代码,应该处理数据,然后保存到一个新文件中

代码如下:

#import regex
import re

#start process_tweet
def processTweet(tweet):
    # process the tweets

    #Convert to lower case
    tweet = tweet.lower()
    #Convert www.* or https?://* to URL
    tweet = re.sub('((www\.[\s]+)|(https?://[^\s]+))','URL',tweet)
    #Convert @username to AT_USER
    tweet = re.sub('@[^\s]+','AT_USER',tweet)
    #Remove additional white spaces
    tweet = re.sub('[\s]+', ' ', tweet)
    #Replace #word with word
    tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
    #trim
    tweet = tweet.strip('\'"')
    return tweet
#end

#Read the tweets one by one and process it
input = open('withoutEmptylines.csv', 'rb')
output = open('editedTweets.csv','wb')

line = input.readline()

while line:
    processedTweet = processTweet(line)
    print (processedTweet)
    output.write(processedTweet)
    line = input.readline()

input.close()
output.close()
我在输入文件中的数据如下所示,因此每条tweet都在一行中:

She wants to ride my BMW the go for a ride in my BMW lol http://t.co/FeoNg48AQZ
BMW Sees U.S. As Top Market For 2015 i8 http://t.co/kkFyiBDcaP
我的函数运行良好,但我对输出不满意,输出如下:

she wants to ride my bmw the go for a ride in my bmw lol URL rt AT_USER Ðun bmw es mucho? yo: bmw. -AT_USER veeergaaa!. hahahahahahahahaha nos hiciste la noche caray! 
tweet number one
tweet number two
tweet number three
因此,它将所有内容放在一行中,而不是像输入文件中的格式那样将每条tweet放在一行中


有人想把每条推文放在一行中吗?

使用如下示例文件:

she wants to ride my bmw the go for a ride in my bmw lol URL rt AT_USER Ðun bmw es mucho? yo: bmw. -AT_USER veeergaaa!. hahahahahahahahaha nos hiciste la noche caray! 
tweet number one
tweet number two
tweet number three
此代码:

file = open('tweets.txt')
for line in file:
   print line
生成此输出:

tweet number one

tweet number two

tweet number three
Python可以很好地读取结束行,但是您的脚本正在通过正则表达式替换来替换它们

此正则表达式替换:

tweet = re.sub('[\s]+', ' ', tweet)
正在将所有空白字符(例如制表符和新行)转换为单个空格

在输出tweet之前,在tweet上添加一个端点,或者修改正则表达式以不替换端点,如下所示:

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

编辑:我将测试替换命令放在那里。建议已修复。

请尝试:
output.write(processedTweet+'\n')
谢谢Ashwini!这解决了我的问题!