Note that there are some explanatory texts on larger screens.

plurals
  1. POsimplejson: loading spanish characters -- utf-8
    primarykey
    data
    text
    <p>I'm trying to load some geographic data with Python's <code>simplejson</code>.</p> <pre><code>&lt;!-- language: lang-py --&gt; string = file("prCounties.txt","r").read().decode('utf-8') d = simplejson.loads(string) </code></pre> <p>The text file has a tilde, the word should be <em>Añasco</em> instead it's <code>u"A\xf1asco"</code> which SimpleJson is not parsing. The source is a <a href="https://github.com/johan/world.geo.json/blob/master/countries/USA/PR/An%CC%83asco.geo.json" rel="nofollow">geoJson file from github</a></p> <pre><code>{"type": "FeatureCollection", "properties": {"kind": "state", "state": "PR"}, "features": [[{"geometry": {"type": "MultiPolygon", "coordinates": [[[[-67.122, 18.3239], [-67.0508, 18.3075], [-67.0398, 18.291], [-67.0837, 18.2527], [-67.122, 18.2417], [-67.1603, 18.2746], [-67.1877, 18.2691], [-67.2261, 18.2965], [-67.1822, 18.3129], [-67.1275, 18.3184]]]]}, "type": "Feature", "properties": {"kind": "county", "name": u"A\xf1asco", "state": "PR"}}]]} </code></pre> <p>Python gives me the error <code>simplejson.decoder.JSONDecodeError: Expecting object</code></p> <hr> <p>The script I used to load from GitHub to generate <code>prCounties.txt</code>. The variable <code>counties</code> is a list of strings related to the locations of the relevant GEOjson data. </p> <p>It's clear this is not the proper way to save this data:</p> <pre><code>&lt;!-- language: lang-py --&gt; countyGeo = [ ] for x in counties: d = simplejson.loads(urllib.urlopen("https://raw.github.com/johan/world.geo.json/master/countries/USA/PR/%s" % (x)).read()) countyGeo += [ d["features"][0]] d["features"][0]=countyGeo file("prCounties.txt", "w").write(str(d)) </code></pre> <p><strong>EDIT</strong>: In the last line, I replaced the <code>str</code> with <code>simplejson.dumps</code>. I guess it encodes properly now. file("prCounties.txt", "w").write(simplejson.dumps(d))</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload