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()