9

I piped the output of my Python script which accesses live twitter tweets to a file output.txt using:

$python scriptTweet.py > output.txt

Originally, the output returned by the script was a dictionary which got written to a text file.

Now i want to use the output.txt file to access tweets stored in it. But when i use this code to parse the text in output.txt into a python dictionary using json.loads():

tweetfile = open("output.txt")
pyresponse = json.loads('tweetfile.read()')
print type(pyresponse)

This error pops up:

    pyresponse = json.loads('tweetfile.read()')
  File "C:\Python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

How should i convert the contents of file output.txt again into a dictionary?

learner123
  • 327
  • 3
  • 9
  • 14

1 Answers1

13

'tweetfile.read()' is a string as you see it. You want to call this function:

with open("output.txt") as tweetfile:
    pyresponse = json.loads(tweetfile.read())

or read it directly using json.load and let json read on the tweetfile itself:

with open("output.txt") as tweetfile:
    pyresponse = json.load(tweetfile)
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 7
    Or use `json.load(tweetfile)`. Why have a dog and bark yourself? – Martijn Pieters May 13 '13 at 12:40
  • @eumiro I updated the code. This error pops up: File "getTweet.py", line 24, in pyresponse = json.load(tweetfile) File "C:\Python27\lib\json\__init__.py", line 278, in load **kw) File "C:\Python27\lib\json\__init__.py", line 326, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 369, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 2 column 1 - line 21 column 1 (char 124 - 56517) – learner123 May 13 '13 at 12:54
  • @techfreak - post an example of your `output.txt` - there might be a problem. – eumiro May 13 '13 at 13:02
  • sample content- {"created_at":"Sun May 12 12:36:43 +0000 2013","id":333561374728925184,"id_str":"333561374728925184","text":"RT `@fatmalonakf: `@AnnisaAndrianii hehe;D","source":"\u003ca href=\"https:\/\/mobile.twitter.com\" rel=\"nofollow\"\u003eMobile Web (M2)\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":354746276,"id_str":"354746276","name":"Annisa Andirani\u2665","screen_name":"AnnisaAndrianii","location":"Bandung_Cimahi ", – learner123 May 13 '13 at 13:13
  • looks like a decoding problem - the error message precises the character. – eumiro May 13 '13 at 13:18
  • @techfreak looks like a you are trying to dump multiple dictionaries (twitter), wrap them in a list, dump the list (instead of dumping dictionaries multiple times) – gogasca Jul 15 '14 at 06:22
  • Might be duplicate of: http://stackoverflow.com/questions/19483351/converting-json-string-to-dictionary-not-list-python if you use loads - check this link out! – El Brutale Nov 26 '15 at 15:59