Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'encoding is an invalid keyword' error inevitable in python 2.x?

Ansi to UTF-8 using python causing error

I tried the answer there to convert ansi to utf-8.

import io  with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:     with open(file_path_utf8, mode='w', encoding='utf-8') as target:         shutil.copyfileobj(source, target) 

But I got "TypeError: 'encoding' is an invalid keyword argument for this function"

I tried with

with io.open(file_path_ansi, encoding='cp1252', errors='ignore') as source: 

, too, and got same error.

Then I tried

import io  with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:     with io.open(file_path_utf8, mode='w', encoding='utf-8') as target:         shutil.copyfileobj(source, target) 

and still got the same error. Also I tried with cp1252, too, but got the same error.

I learned from several stackoverflow questions that

TypeError: 'encoding' is an invalid keyword argument for this function 

is frequently arising error message in python 2.x

But mainly answerers were suggesting using python 3 in some way or the other.

Is it really impossible to convert ansi txt to utf-8 txt in python 2.x ? (I use 2.7)

like image 438
user3123767 Avatar asked Jul 31 '14 02:07

user3123767


2 Answers

For Python2.7, Use io.open() in both locations.

import io import shutil  with io.open('/etc/passwd', encoding='latin-1', errors='ignore') as source:     with io.open('/tmp/goof', mode='w', encoding='utf-8') as target:         shutil.copyfileobj(source, target) 

The above program runs without errors on my PC.

like image 183
Robᵩ Avatar answered Sep 20 '22 08:09

Robᵩ


This is how you can convert ansi to utf-8 in Python 2 (you just use normal file objects):

with open(file_path_ansi, "r") as source:     with open(file_path_utf8, "w") as target:         target.write(source.read().decode("latin1").encode("utf8")) 
like image 30
Thomas Hobohm Avatar answered Sep 19 '22 08:09

Thomas Hobohm