Скажите геойсону сохранить данные как UTF8? - PullRequest
0 голосов
/ 29 августа 2018

Я не могу понять, почему geojson сохраняет данные в этом формате (UTF16?), Так что «François» заканчивается как «Fran \ u00e7ois» при вызове geojson.dump () для сохранения данных на диск.

Есть идеи?

import geojson
from geojson import LineString, Point, Feature, FeatureCollection, dump
from geopy.geocoders import Nominatim

with open('input.geojson', encoding='utf-8') as f:
    gj = geojson.load(f)

for track in gj['features']:
    #NO DIFF with open(track['properties']['name'][0] + '.geojson', 'a+', encoding='utf-8') as f:
    with open(track['properties']['name'][0] + '.geojson', 'a+') as f:
        dump(track, f, indent=2)

        #UnicodeEncodeError: 'charmap' codec can't encode character '\u2194' in position 7: character maps to <undefined>
        #dump(track, f, indent=2, ensure_ascii=False)

        #NOT DEFINED
        #dumps(track, f, indent=2)

        #AttributeError: encode
        #dump(track.encode("utf-8"), f, indent=2, ensure_ascii=False)

Спасибо.

-

Редактировать:

Я проследил за другим потоком и попробовал несколько вещей, но все еще не могу сохранить исходный текст.

        INPUTFILE = 'input.geojson'

        with open(INPUTFILE, encoding='utf-8') as f:
            gj = geojson.load(f)

        for track in gj['features']:
            #Bad : \u00e0 instead of "à"
            ##with open(track['properties']['name'][0] + '.geojson', 'a+') as f:
                ##dump(track, f, indent=2)

            #Bad : "Ã " instead of "à"
        ##    with io.open(track['properties']['name'][0] + '.geojson', 'a+', encoding='utf8') as json_file:
        ##        dump(track, json_file, ensure_ascii=False)

        ##    with io.open(track['properties']['name'][0] + '.geojson', 'a+', encoding='utf8') as json_file:
        ##        #NameError: name 'dumps' is not defined
        ##        data = dumps(track, ensure_ascii=False)
        ##        # unicode(data) auto-decodes data to unicode if str
        ##        json_file.write(unicode(data))

            #Bad : "Ã " instead of "à"
        ##    with codecs.open(track['properties']['name'][0] + '.geojson', 'a+', encoding='utf-8') as f:
        ##        dump(track, f, ensure_ascii=False)

    ##    with codecs.open(track['properties']['name'][0] + '.geojson', 'a+', 'utf-8') as fp:
    ##        #NameError: name 'dumps' is not defined
    ##        fp.write(dumps(track, ensure_ascii=False))

    #Bad : "Ã " instead of "à"
##    with io.open(track['properties']['name'][0] + '.geojson', 'a+', encoding='utf8') as json_file:
##        json.dump(track, json_file, ensure_ascii=False)

            break
...