2004年03月11日 星期四 12:29
您好!
for line in codecs.open(filename, mode, encoding):
print line
上面的代码运行时产生“NotImplementedError”
改成下面形式就好了:
ls = codecs.open('pt.txt', 'r', 'utf16').readlines()
for l in ls:
print l
读出的数据是unicode对象
======= 2004-03-11 11:57:18 您在来信中写道:=======
>请看这儿:http://mail.python.org/pipermail/python-list/2003-June/thread.html#167677
>关于"read a unicode file"的几封信。
>
>-------
>Explicit is better than implicit ...
>
>-----Original Message-----
>From: 0.706 [mailto:0.706 at 163.com]
>Sent: 2004年3月11日 10:57
>To: python-chinese at lists.python.cn
>Subject: Re: Re: Re: RE: [python-chinese] (no subject)
>
>
>大家好!
>
> 应该是unicode,那应该如何?
>
>======= 2004-03-11 10:52:07 您在来信中写道:=======
>
>>0.706,您好!
>>
>> 是编码的问题吧。
>>原先的文件是什么编码?unicode
>>
>>======= 2004-03-11 10:47:37 您在来信中写道:=======
>>
>>>Jacob Fan,您好!
>>>
>>> 没有报告错误,但是生成的结果文件不对,pt.txt有360k,
>>>生成的ptz.txt只有1K,而且有乱码.
>>>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = =
致
礼!
zhao
bear_sh at eastday.com
2004-03-11
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fox.gif
Type: image/gif
Size: 9519 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20040311/b5f5a38a/fox.gif
2004年03月11日 星期四 13:01
Jacob Fan,您好!
多谢指点,程序改成下面可以了。但是如果把'utf16'改成'gbk'就出错,
能解释一下'utf16''utf8''gbk''gb2312'之间的区别吗?再就是怎么判断文件
该用哪方式?
# -*- coding: cp936 -*-
import codecs
oldfile=codecs.open("pt.txt","r","utf16")
newfile=codecs.open("ptz.txt","w",'utf16')
lines=oldfile.readlines()
for line in lines:
newfile.write(line)
oldfile.close()
newfile.close()
======= 2004-03-11 11:57:18 您在来信中写道:=======
>请看这儿:http://mail.python.org/pipermail/python-list/2003-June/thread.html#167677
>关于"read a unicode file"的几封信。
>
>-------
>Explicit is better than implicit ...
>
>-----Original Message-----
>From: 0.706 [mailto:0.706 at 163.com]
>Sent: 2004年3月11日 10:57
>To: python-chinese at lists.python.cn
>Subject: Re: Re: Re: RE: [python-chinese] (no subject)
>
>
>大家好!
>
> 应该是unicode,那应该如何?
>
>======= 2004-03-11 10:52:07 您在来信中写道:=======
>
>>0.706,您好!
>>
>> 是编码的问题吧。
>>原先的文件是什么编码?unicode
>>
>>======= 2004-03-11 10:47:37 您在来信中写道:=======
>>
>>>Jacob Fan,您好!
>>>
>>> 没有报告错误,但是生成的结果文件不对,pt.txt有360k,
>>>生成的ptz.txt只有1K,而且有乱码.
>>>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
= = = = = = = = = = = = = = = = = = = =
致
礼!
0.706
0.706 at 163.com
2004-03-11
2004年03月11日 星期四 13:36
>能解释一下'utf16''utf8''gbk''gb2312'之间的区别吗? 这都是不同的编码方式。前面两种是unicode的两种编码方式,'gb2312'是中国汉字编码国家标准, gbk是一个扩展了的gb编码集。所以,你得先搞清楚自己的文件是哪种编码。 ------- Explicit is better than implicit ... -----Original Message----- From: 0.706 [mailto:0.706 at 163.com] Sent: 2004年3月11日 13:01 To: python-chinese at lists.python.cn Subject: Re: RE: Re: Re: RE: [python-chinese] (no subject) Jacob Fan,您好! 多谢指点,程序改成下面可以了。但是如果把'utf16'改成'gbk'就出错, 能解释一下'utf16''utf8''gbk''gb2312'之间的区别吗?再就是怎么判断文件 该用哪方式? # -*- coding: cp936 -*- import codecs oldfile=codecs.open("pt.txt","r","utf16") newfile=codecs.open("ptz.txt","w",'utf16') lines=oldfile.readlines() for line in lines: newfile.write(line) oldfile.close() newfile.close()
Zeuux © 2025
京ICP备05028076号