0

I collect data from a website, using the pycurl functions. Following the example code, after slight modifications I do get all the data in one big buffer. It comes in a format of { "field_id":"data","field_id2":"data2", .. } I wish to parse this into lines of text, similar to csv, with one line of output for each set of data enclosed by curly braces. How best to achieve this, as "pythonic" as can be? Oh, and where can I best do the UTF-8 conversion? I suspect the source coding to be ISO8859 - but that is a secondary matter.

Relevant code:

buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, MY_URL)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

Sample data:

`{"status":"ok","nature":"Liste des champs principaux de tous les terrains publi\u00e9s","count":"1211","liste":
 [{"id":"3667","date_modif":"2013-04-03 11:27:00","code_terrain":"LF5155","toponyme":"Champagne Vol Libre","type_terrain":"Base ULM Autorisation OBLIGATOIRE ","latitude":"N 48 56 10","longitude":"E 004 03 42","altitude":"335` ft","pays":"France","region":"Grand-Est","departement":"Marne","ville":"Villeneuve"},
{"id":"5118","date_modif":"2015-08-18 13:17:23","code_terrain":"LF4861","toponyme":"La Canourgue Conques","type_terrain":"A\u00e9rodrome ferm\u00e9.","latitude":"N 44 24 55","longitude":"E 003 17 05","altitude":"2920 ft","pays":"France","region":"Occitanie","departement":"Loz\u00e8re","ville":"La Canourgue"}
Karel Adams
  • 185
  • 2
  • 19
  • Thanks, PRMoureu but that doesn't work. Perhaps I am missing something elementary, as already said JSON is new terrain for me. The error is "ValueError: No JSON object could be decoded" – Karel Adams Oct 28 '17 at 08:57
  • how did you get the sample output ? you could use the same way with `json.loads(result_string)` – PRMoureu Oct 28 '17 at 09:02

2 Answers2

1

You are trying to parse a JSON file, maybe will be useful to look at the documentation https://docs.python.org/2/library/json.html

Adriano
  • 3,788
  • 5
  • 32
  • 53
  • Ah! I had never yet come across JSON formatted data, that's why I didn't recognise the format. – Karel Adams Oct 28 '17 at 06:51
  • Don't forget to "accept" my answer so also other people coming to here will find the solution and will "trust" it. :) – Adriano Oct 28 '17 at 09:02
0
import json
result = json.loads('{"something": 1}', encoding="ISO8859")  # default encoding is utf-8
print(result["something"])

>>> 1
Mojtaba Kamyabi
  • 3,440
  • 3
  • 29
  • 50
  • Whilst code only answers may solve the problem, it is always better if you can explain why your answer solves the question. This helps when others view the answer to understand what has changed and why it should be done this way. – Nigel Ren Oct 28 '17 at 10:36