Ansi到UTF-8使用python导致错误 [英] Ansi to UTF-8 using python causing error
问题描述
在我尝试编写一个将Ansi转换为UTF-8的python程序时,我发现这个
它将UTF-8转换为Ansi。
我以为只要扭转订单就可以了。所以我编码了
file_path_ansi =input.txt
file_path_utf8 =output.txt
b $ b #open并编码原始内容
file_source = open(file_path_ansi,mode ='r',encoding ='latin-1',errors ='ignore')
file_content = file_source.read )
file_source.close
#write
file_target = open(file_path_utf8,mode ='w',encoding ='utf-8')
file_target.write file_content)
file_target.close
但它会导致错误。
TypeError:file<>最多取3个参数< 4给定>
所以我改变了
file_source = open(file_path_ansi,mode ='r',encoding ='latin-1',errors ='ignore')
到
file_source = open(file_path_ansi,mode ='r',encoding = 'latin-1')
然后会导致另一个错误:
TypeError:'encoding'是此函数的无效关键字argumentemtn
我应该如何修复我的代码来解决这个问题?
解决方案3版本的
open()
函数,在Python 2.在主要版本之间,I / O支持被大修,支持更好的编码和解码。
Python 2中的版本为
io.open()
。
我会使用
$ igbshutil.copyfileobj()
函数进行复制,因此您不必将整个文件读入内存:
$ bimport io
使用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 (源,目标)
大多数谈到ANSI的人都指的是 Windows代码页的一个;你可能真的有一个文件在CP(代码页)1252,这是几乎,但不完全相同的事情 ISO-8859-1(拉丁语1)。如果是,使用
cp1252
而不是latin-1
作为编码
参数。While I was trying to write a python program that converts Ansi to UTF-8, I found this
How can I convert UTF-8 to ANSI in Python
which converts UTF-8 to Ansi.
I thought it will just work by reversing the order. So I coded
file_path_ansi = "input.txt" file_path_utf8 = "output.txt" #open and encode the original content file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore') file_content = file_source.read() file_source.close #write file_target = open(file_path_utf8, mode='w', encoding='utf-8') file_target.write(file_content) file_target.close
But it causes error.
TypeError: file<> takes at most 3 arguments <4 given>
So I changed
file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore')
to
file_source = open(file_path_ansi, mode='r', encoding='latin-1')
Then it causes another error:
TypeError: 'encoding' is an invalid keyword arguemtn for this function
How should I fix my code to solve this problem?
解决方案You are trying to use the Python 3 version of the
open()
function, on Python 2. Between the major versions, I/O support was overhauled, supporting better encoding and decoding.You can get the same new version in Python 2 as
io.open()
instead.I'd use the
shutil.copyfileobj()
function to do the copying, so you don't have to read the whole file into memory: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)
Be careful though; most people talking about ANSI refer to one of the Windows codepages; you may really have a file in CP (codepage) 1252, which is almost, but not quite the same thing as ISO-8859-1 (Latin 1). If so, use
cp1252
instead oflatin-1
as theencoding
parameter.这篇关于Ansi到UTF-8使用python导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!