0

I use this code to get the tweet from the feed which i write inside a file. When i read the file and try to json the lines i always get an ERROR.

    def SearchTwt(api):
    os.chdir('/Users/me/Desktop')
    SearchResult = api.search( q='market',lang='en',rpp=20)
    text_file = open("TweetOut.txt", "w")
    for tw in SearchResult:
        text_file.write(str(tw))
        print(str(tw))
    text_file.close()

I read the file with:

    def readfile():
    tweets_data = []
    os.chdir('/Users/me/Desktop')
    file = open("TweetOut.txt", "r")

    for line in file:
        parts = line.split("Status(")

        print (len(parts))
        for part in parts:
            tweet = 'Status('+part
            if len(tweet) > 10:
                tweetj = json.loads(tweet)
                #tweets_data.append(tweet)
                print(tweet)
    file.close()

May be this is wrong to fill the file with str(tw)? Yes I rebuild the string during the reading because i thought the tweet started like that. So may be another mistake.

I tried a lot of other options.

the error: raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

the file starts like this (edited the url as asked by stack): Status(source='SocialFlow', id=757991135465857024, in_reply_to_status_id=None, is_quote_status=False, entities={'hashtags': [], 'user_mentions': [], 'symbols': [], 'urls': [{'url': '', 'expanded_url': '', 'display_url':

1 Answers1

1

The file is not valid JSON. It should be something like

{
  "source": "SocialFlow",
  "id":"757991135465857024",
  ...
  "entities": {
    "hashtags": [], 
    "user_mentions": [],
    ...
  }
}

Because it is not valid json you either have to parse it a different way, or be sure to write it as json when you save the file.

MiltoxBeyond
  • 2,683
  • 1
  • 13
  • 12
  • Ok i Save the file as txt. i will try this one: http://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file-in-python –  Jul 27 '16 at 01:18
  • did not go far with this code: json.dump(SearchResult, text_file, ensure_ascii=False) raise TypeError(repr(o) + " is not JSON serializable") –  Jul 27 '16 at 01:30
  • I didn't give you code...it was just an observation that your JSON was wrong. Plus I don't know all the JSON you have stored so I can't help you with actual code... that's why I used the three dots to indicate fill in the blanks – MiltoxBeyond Jul 27 '16 at 01:32
  • don't worry I tried a lot of different code on this one. I can easily split() the full twit automatically but decided that json will be more elegant. Adding {} did not help either. –  Jul 27 '16 at 01:36
  • It is just that you are not saving it as JSON. It is invalid. If it were json you wouldn't need to do any splitting just read all of it and parse it with json.loads() – MiltoxBeyond Jul 27 '16 at 01:39