So I’m working with the twitter API of a soccer team, and I want to write to a file only the tweets tweeted during a live game, where the tweets are minute by minute events of the game. For instance, “25′ This player got a yellow card” is an example of a tweet im looking for. All the live game tweets start with a minute, hence an integer, and I use the following regex to obtains such tweets, and write them to an text file
tweets = client.user_timeline('ManUtd', count: 3200)
tweets_file = File.open("tweets_file.txt", 'w')
tweets.each do |tweet|
if /[0-9]/.match(tweet.full_text[0])
tweets_file.write(tweet.full_text + "n")
end
end
The thing is, my text file looks exactly like I want it to look like after I add in those tweets, but, the File.size() is 0, and I can’t perform a code block on it either, that is, I cant do File.Open(tweets_file).each
and evaluate each line or word in the file, because, as the file size indicates, there’s nothing in it, but the actual file has a bunch of tweets written in it, so I’m pretty confused.
Can anyone shed some light on whats going on here?
EDIT: I forgot to mention, if I remove the regular expression, and simply add all the tweets form the timeline to my file, or if i replace the regex by [A-Z], then the File.Size works fine and i can use the each method on File.Open so it seems it has something to do with the integers at the beginning of a sentence
2
Answers
Most likely you have a whitespace character or some kind of prefix string in the beginning of the tweet line.
Use:
This will strip away whitespaces. Else you should find the prefix and strip IT out.
I would recommend separating the types of operations (and I’m pretty sure this would solve your problem), something like this:
Using
File.write
and not usingFile.open
greatly simplifies the task since you don’t have to worry about closing the file.