简体   繁体   中英

sublime text3 is not displaying utf8 character but their codes

I'm dealing with some german text data. I tried to save some metadata into a JSON file in python.

Here is how the metadata looks like(as you could notice, there are ä, ö characters):

{'pages': ['kitchwitch.ch_posts.csv',
  'Sujetbaizinbasel_posts.csv',
  'Guggenmusik-Moore-Schränzer-149777592206118_posts.csv',
  'zueundrichproductions_posts.csv',
  'glueckshof_posts.csv',
  'Lieberockt_posts.csv',
  'Pfadi-Schöftle-699078857125683_posts.csv'...
with open('./gsw_fb_r1_metadata.json','w',encoding='utf8' )as f:
    json.dump(metadata,f)

But when I open it with Sublime Text3, I get the following:

{"pages": ["kitchwitch.ch_posts.csv", "Sujetbaizinbasel_posts.csv", 
"Guggenmusik-Moore-Schra\u0308nzer-149777592206118_posts.csv", 
"zueundrichproductions_posts.csv", "glueckshof_posts.csv", 
"Lieberockt_posts.csv", "Pfadi-Sch\u00f6ftle-699078857125683_posts.csv", 

So the ö and ä are not displayed properly. How could I get them displayed normally as in Python? Is this problem linked to Sublime Text or to me? Thanks!

That is valid JSON. The default is to write the data using only ASCII and Unicode escape codes. When you load the JSON back into Python with json.load() the strings will contain the original characters again.

If you want the JSON text file to be readable instead of using escape codes, you can use ensure_ascii=False :

with open('./gsw_fb_r1_metadata.json','w',encoding='utf8' )as f:
    json.dump(metadata,f,ensure_ascii=False)

This will write the data as UTF-8 to the file and be readable in a UTF-8-capable text editor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM