简体   繁体   中英

While appending to text file, non-english characters get corrupted

I am trying to append some text to a js file, and it contains non-english characters like 'ç,ş,ı'.

Appending text is dinamic, when it contains a non-english character, the non-english characters in the js file corrupts.

string = '{ "type": "Feature", "properties": { "color": "' + colors[color] + '", "geometry": "Point", "name": " ' + user_input + '", "description": "' + desc + '" }, "geometry": { "type": "Point", "coordinates": [ ' + str(lng) + ', ' + str(lat) + ' ] } },]};'

f = open('mapdata.js', 'a')
f.write(string)
f.close()

If you try to write to a file with unicode strings, When you write a unicode string to a file, python tries to encode it in ASCII. That's why the the text is corrupted. To fix that encode the text to utf-8 and open the file as binary.

string = '{ "type": "Feature", "properties": { "color": "' + colors[color] + '", "geometry": "Point", "name": " ' + user_input + '", "description": "' + desc + '" }, "geometry": { "type": "Point", "coordinates": [ ' + str(lng) + ', ' + str(lat) + ' ] } },]};'

f = open('mapdata.js', 'ab')
string.encode('utf-8')
f.write(string)
f.close()

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