How To Get Only The Text Of The Tweets Into A Json File
Solution 1:
First thing you need to do is change the below code as:
defsave_to_json(obj, filename):
withopen(filename, 'a') as fp:
json.dump(obj, fp, indent=4, sort_keys=True)
You need to change the mode in which file is open because of the below reason.
w:
Opens in write-only mode. The pointer is placed at the beginning of the file and this will overwrite any existing file with the same name. It will create a new file if one with the same name doesn't exist.
a:
Opens a file for appending new information to it. The pointer is placed at the end of the file. A new file is created if one with the same name doesn't exist.
Also, there is no meaning of sort_keys
as you are only passing a string
and not a dict
. Similarly, there is no meaning of indent=4
for strings
.
If you need some indexing with the tweet text you can use the below code:
tweets = {}
for i, tweet inenumerate(x1_tweets):
tweets[i] = tweet['text']
save_to_json(tweets,'bat.json')
The above code will create a dict
with index to the tweet and write to the file once all tweets are processed.
And if you just need the text of the tweets without the index you can use string aggregation
or use list
and append
all the text from tweet in that list
and write that to the output file.
Post a Comment for "How To Get Only The Text Of The Tweets Into A Json File"