I have a very large txt file to read and process it, but as I'm new at Python, I don't know what is the format of the file and how can I can read it. Below there is a sample:
[
{"content": "111111", "n": "ITEM 1", "a": "ANOTHER", "t": 1},
{"content": "222222", "n": "ITEM 2", "a": "ANOTHER", "t": 1},
{"content": "333333", "n": "ITEM 3", "a": "ANOTHER", "t": 1}
]
So, I need to take a loop each item inside the list '[]' (what I think I did), and then, each item like "content", "n", "a", "t".
I tried to read the file and take a loop like this:
for item in thecontent:
data = json.load(item)
pprint(data)
I think I got each 'item' on the loop above as a string, not as json.
Edit 2 I think that I need to use the ujson data type, as the sample I got at the documentation is the same here, above. If you want to know better, go to the documentation page
>>> import ujson
>>> ujson.dumps([{"key": "value"}, 81, True])
'[{"key":"value"},81,true]'
>>> ujson.loads("""[{"key": "value"}, 81, true]""")
[{u'key': u'value'}, 81, True]
Thanks everyone!
Edit 3: I kept looking for any answer about the problem I had, and just found that the problem wasn't about 'how to read' a list or tuples, because I did this by the file.
The main problem was about how to convert bytes to string when get the content from web, and I solve it in this topic, more specifically at this reply.
The code I wrote to get the webcontent and convert it to json is that:
def get_json_by_url(url):
r = requests.get(url)
r.raise_for_status()
return json.loads(r.content.decode('utf-8'))
So, as maybe this is a solution for anyone who is looking for this, I've changed the title from 'How to read a list of tuples (or json) in python' to 'How to get content from web and convert from bytes to str/json' wich was the problem I got.
I'm sorry about not to explain very well the problem, so as I'm new at Python, sometimes it takes a lot of time to diagnose what is the problem itself.
Thanks all!