Python yaml file written Chinese

Look yaml file writing, as follows:

yaml Import 
Import OS 

curpath = os.path.dirname (the os.path.realpath (__ file__)) Get File # current path 
yamlpath = the os.path.join (curpath, " data.yaml " ) # yaml Get File Address 

Data = { ' name ' : ' test ' } # require incoming data 
with Open (yamlpath, ' W ' , encoding = ' UTF-. 8 ' ) AS F: 
    yaml_obj = yaml.dump (data, F)

After running, view the file yaml

 

 

 Chinese characters are transcoded

with open(yamlpath, 'w', encoding='utf-8') as f:
    yaml_obj = yaml.dump(data, f, allow_unicode=True)

Add allow_unicode = True, run again

 

 Save Chinese success.

View source

 

 The method of processing data in the found dump_all view dump_all source, which can be seen a variety of conditions may be provided

 

 

with open(yamlpath, 'w', encoding='utf-8') as f:
    yaml_obj = yaml.dump(data, f,allow_unicode=True,sort_keys=False)

Such as setting sort_keys = False, the write data is written after yaml is not sorted, if additional writing, read mode into 'a' can be a

 

Guess you like

Origin www.cnblogs.com/jescs/p/12155022.html