python open encoding ansi_使用python从Ansi到UTF-8

当我试图编写一个将Ansi转换为UTF-8的python程序时,我发现

将UTF-8转换为Ansi。

我想只要颠倒顺序就行了。所以我编码了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

但这会导致错误。TypeError: file<> takes at most 3 arguments <4 given>

所以我改变了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' is an invalid keyword arguemtn for this function

我应该如何修改代码来解决这个问题?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值