Sunday, 15 April 2012

python - Stripping Line Breaks in Tweets via Tweepy -



python - Stripping Line Breaks in Tweets via Tweepy -

i'm looking pull info twitter api , create pipe separated file can farther processing on. code looks this:

auth = tweepy.oauthhandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.api(auth) out_file = "tweets.txt" tweets = api.search(q='foo') o = open(out_file, 'a') tweet in tweets: id = str(tweet.id) user = tweet.user.screen_name post = tweet.text post = post.encode('ascii', 'ignore') post = post.strip('|') # pipes in tweets don't create unwanted separators post = post.strip('\r\n') record = id + "|" + user + "|" + post print>>o, record

i have problem when user's tweet includes line breaks makes output info this:

473565810326601730|usera|this tweet 473565810325865901|userb|some other illustration 406478015419876422|userc|line separated tweet 431658790543289758|userd|one more tweet

i want strip out line breaks on 3rd tweet. i've tried post.strip('\n') , post.strip('0x0d 0x0a') in add-on above none seem work. ideas?

that because strip returns "a re-create of string leading , trailing characters removed".

you should utilize replace new line , pipe:

post = post.replace('|', ' ') post = post.replace('\n', ' ')

python twitter tweepy

No comments:

Post a Comment