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?
#8
It's a Windows editor that's been around for about 25 years.

I have yet another encoding issue, this time with geojson :-/

If I use its dump(), UTF8 data is turned into UTF16 (apparently), eg. "Fran\u00e7ois" instead of "François":

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)
As shown, I found and tried several things, all to no avail.

Should I use another method than "dump"?
Reply


Messages In This Thread
RE: Right way to write string into UTF-8 file? - by Winfried - Aug-29-2018, 07:06 AM

Forum Jump:

User Panel Messages

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