How to set encoding as 'ANSI' using Python?

109
November 28, 2019, at 5:20 PM

I am using Python 3.7.4 version. I want to set the encoding as 'ANSI' at the time of reading a text file and also writing a text file.

I another case I read a file by providing 'utf-8' ( please find code snippet below ) as encoding but in case of 'ANSI' I am not finding any value to provide as encoding.

code snippet :

content = open(fullfile , encoding='utf-8').readlines()  

What should be done to set encoding as 'ANSI' in Python ?

Answer 1

ANSI is not actually an encoding, you probably mean Windows-1252, which Python supports as 'cp1252'.

Answer 2

There is no "ANSI"-encoding. "ANSI" means "whatever the default single-byte encoding happens to be on your machine" – the term "ANSI" is inherently ambiguous. This means you must specify an actual encoding when reading the file.

For Windows machines in the Western Europe region, "ANSI" typically refers to Windows-1252. Other regions differ, but also your machine configuration might be different.

Python refers to Windows-1252 as cp1252. If that really is the encoding your file is in depends on the file itself, and can only be found out by looking at it.

Often text editors (not Notepad, real text editors) have an option to interpret a file in various encodings. Pick the one that makes the data look right (pay attention to accented characters) and then find out Python's name for it.

Answer 3

Try one of the ANSI encodings:

encoding='cp1252'

For further information, take a look here

Rent Charter Buses Company
READ ALSO
Difference between AMP and AMQP?

Difference between AMP and AMQP?

The Advanced Message Queuing Protocol (AMQP) and the Asynchronous Messaging Protocol (AMP), as their name already says, are protocols

143
Grouping by date and number of unique users for multiple variables

Grouping by date and number of unique users for multiple variables

I have a dataframe containing tweetsI've got columns with information about the datetime, about a unique user_id and then columns indicating if the tweet belongs to a thematic category

99
Why does scipy.griddata return nans with 'cubic' interpolation if input 'values' contains nan?

Why does scipy.griddata return nans with 'cubic' interpolation if input 'values' contains nan?

I want to perform cubic interpolation of an array that contains some nan values using scipygriddata

123