python codecs_Python:如何使用codecs模块将unicode数据保存成gbk格式

需求:使用codecs模块将utf-8的文件保存成gbk格式。

读取的时候,已经将编码设置成utf-8了。输出结果是unicode字符串。

但是在将unicode字符串写入文件并保存为gbk的时候,发现文件内容为空(0kb)。

注:测试的时候,发现有的文件转码的时候,文件变小了,而且内容被截断。

比如:原本

1a.txt

的内容为:

1<div><table> </table></div>

但转码后变成:

1<div><table>

原本以为是

1NUL

字符的问题,但后面替换了NUL字符,发现结果还是一样。

python代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32#coding:utf-8

import os

import codecs

def ReadFile(filePath,encoding="utf-8"):

try:

strContent = ""

f = codecs.open(filePath,"r",encoding=encoding)

line = f.readline()

while(line):

strContent += line

line = f.readline()

f.close()

return strContent

except Exception,ex:

return None

def WriteFile(filePath,u,encoding="gb2312"):

try:

f = codecs.open(filePath,"w",encoding)

f.writelines(u)

f.flush()

f.close()

except Exception,ex:

pass

def UTF8_2_GBK(src,dst):

try:

content = ReadFile(src,encoding="utf-8")

WriteFile(dst,content,"gb2312")

except Exception,ex:

pass

代码太 C-ish、Java-ish 了。从你忽略这么多异常来看,你根本就是找死:

1ReadFile

返回的是字符串,但是你在

1WriteFile

里把它当列表了

明明有

1.read()

方法一次读取整个文件内容、

1.readlines()

作为行的列表读入,为什么不用?

没事别学一些不会编程的 Javaers,不要抓你不知道如何处理的异常。难道你不小心写错了语法你不希望它直接告诉你,而喜欢错误和你玩捉迷藏吗?

1python

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16#coding:utf-8

import os

import codecs

def ReadFile(filePath, encoding):

with codecs.open(filePath, "r", encoding=encoding) as f:

return f.read()

def WriteFile(filePath, content, encoding):

with codecs.open(filePath, "w", encoding=encoding) as f:

f.write(content)

def UTF8_to_GBK(src, dst):

content = ReadFile(src, encoding="utf-8")

WriteFile(dst, content, "gbk")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值