Python Forum
Right way to write string into UTF-8 file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right way to write string into UTF-8 file?
#10
(Aug-29-2018, 07:06 AM)Winfried Wrote: Should I use another method than "dump"?
Not sure because don't know where i goes wrong,don'not have file you use.
You should not use f in both read and dump.

Here a example where i use a geojson file and put in François.
import json
from pprint import pprint

with open('map.geojson' , encoding='utf-8') as f:
    data = json.load(f)
    pprint(data)
Output:
{'features': [{'geometry': {'coordinates': [[8.9208984375, 61.05828537037916], [9.84375, 61.4597705702975], [10.7666015625, 60.930432202923335]], 'type': 'François'}, 'properties': {}, 'type': 'Feature'}], 'type': 'FeatureCollection'}
Dump data.
import json

with open("data_file.json", "w", encoding='utf-8') as f_out:
    json.dump(data, f_out, ensure_ascii=False
Content of data_file.json.
Output:
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "François", "coordinates": [ [ 8.9208984375, 61.05828537037916 ], [ 9.84375, 61.4597705702975 ], [ 10.7666015625, 60.930432202923335 ] ] } } ] }
Reply


Messages In This Thread
RE: Right way to write string into UTF-8 file? - by snippsat - Aug-29-2018, 06:22 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020