일부 지리 데이터를 Python의 simplejson
으로로드하려고합니다.simplejson : 로딩 스페인어 문자 - utf-8
<!-- language: lang-py -->
string = file("prCounties.txt","r").read().decode('utf-8')
d = simplejson.loads(string)
텍스트 파일 물결표를 가지고, 단어 Añasco 대신이 SimpleJson 구문 분석되지 u"A\xf1asco"
있어해야합니다. 소스는 geoJson file from github
{"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"}}]]}
파이썬은 나에게 오류 simplejson.decoder.JSONDecodeError: Expecting object
내가 prCounties.txt
를 생성하는 GitHub의에서로드하는 데 사용되는 스크립트를 제공합니다. 변수 counties
은 관련 GEOjson 데이터의 위치와 관련된 문자열 목록입니다.
이 문제는이 데이터를 저장하는 적절한 방법이 아니다 분명하다 :
<!-- language: lang-py -->
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))
편집 : 마지막 라인에, 내가 simplejson.dumps
와 str
를 교체했다. 나는 그것이 지금 적절하게 암호화한다고 생각한다. 파일 ("prCounties.txt", "w") write (simplejson.dumps (d))
'u는 "A가 xf1asco \"'같은 문자열'과 같다 : 그럼 당신은)합니다 (simplejson.loads 읽을 기능 형식으로 변수 "문자열"을 만들기 위해 잘 작동이 코드를 사용할 수 있습니다 u "Añasco"(및 "u"A \ u00f1asco "). – abarnert
당신이'u '로 보는 이유는 \ xf1asco "'(파이썬 2.x에서) 유니 코드 문자열의'repr'이 비 ASCII 문자를 이스케이프한다는 것입니다. 예를 들어, 인터프리터 인터프리터에서'u'''''''는'u' \ xf1''을 출력하지만'print''''''는'ñ'를 출력합니다. – abarnert
또 다른 이유는
simplejson
이 제대로로드되지 않았기 때문입니까? –