Convert encoding of a text file from utf-8 to ansi or unicode in python
Asked Answered
F

2

5

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?

Fingertip answered 25/12, 2016 at 9:50 Comment(5)
possible duplicate of #4300175Incisor
A precision: unicode is a characters set; utf8 is a codec (an algorithm) used to encode Unicode characters.Giovannagiovanni
@LaurentLAPORTE, but when in windows we want to save as a text file, in encoding options we can see both utf-8 and unicodes.!?Fingertip
See https://mcmap.net/q/25700/-what-is-ansi-format for a clear description of what is ANSI charset.Giovannagiovanni
See https://mcmap.net/q/48007/-what-are-unicode-utf-8-and-utf-16 to have a clear description of what is Unicode charset.Giovannagiovanni
V
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)
Veroniqueverras answered 24/8, 2017 at 17:52 Comment(0)
G
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)
Giovannagiovanni answered 25/12, 2016 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.