I have a text file with utf-8 encoding. I want to change it's unicode to ANSI or unicode automatically in python. Is it possible? How can i do it?
Asked
Active
Viewed 2.8k times
5
-
2possible duplicate of http://stackoverflow.com/questions/4299675/python-script-to-convert-from-utf-8-to-ascii – Binyamin Even Dec 25 '16 at 09:54
-
A precision: **unicode** is a characters set; **utf8** is a codec (an algorithm) used to encode Unicode characters. – Laurent LAPORTE Dec 25 '16 at 09:54
-
@LaurentLAPORTE, but when in windows we want to save as a text file, in encoding options we can see both utf-8 and unicodes.!? – narges Dec 25 '16 at 10:08
-
See http://stackoverflow.com/a/701920/1513933 for a clear description of what is ANSI charset. – Laurent LAPORTE Dec 25 '16 at 10:45
-
See http://stackoverflow.com/a/15128103/1513933 to have a clear description of what is Unicode charset. – Laurent LAPORTE Dec 25 '16 at 10:45
2 Answers
7
Try this
#read input file
with codecs.open('USERS.CSV', 'r', encoding = 'latin-1') as file:
lines = file.read()
#write output file
with codecs.open('1_UserPython.CSV', 'w', encoding = 'utf_8_sig') as file:
file.write(lines)

Alciomar Hollanda
- 865
- 8
- 7
4
To convert a file from utf8 to cp1252:
import io
with io.open(src_path, mode="r", encoding="utf8") as fd:
content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
fd.write(content)

Laurent LAPORTE
- 21,958
- 6
- 58
- 103