A question about unicode() function
Hi,all I encountered a problem when using unicode() function to fetch a webpage, I don't know why this happenned. My codes and error messages are: Code: #!/usr/bin/python #Filename: test.py #Modified: 2006-12-31 import cPickle as p import urllib import htmllib import re import sys def funUrlFetch(url): lambda url:urllib.urlopen(url).read() objUrl = raw_input('Enter the Url:') content = funUrlFetch(objUrl) content = unicode(content,"gbk") print content content.close() error message: C:\WINDOWS\system32\cmd.exe /c python test.py Enter the Url:http://www.msn.com Traceback (most recent call last): File "test.py", line 16, in ? content = unicode(content,"gbk") TypeError: coercing to Unicode: need string or buffer, NoneType found shell returned 1 Hit any key to close this window... Any suggestions would be appreciated! Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about unicode() function
Hi, I changed my codes to: #!/usr/bin/python #Filename: test.py #Modified: 2007-01-01 import cPickle as p import urllib import htmllib import re import sys funUrlFetch = lambda url:urllib.urlopen(url).read() objUrl = raw_input('Enter the Url:') content = funUrlFetch(objUrl) content = content.encode('gb2312','ignore') print content content.close() I used "ignore" to deal with the data lose, but it still caused a error: C:\WINDOWS\system32\cmd.exe /c python tianya.py Enter the Url:http://www.tianya.cn Traceback (most recent call last): File "tianya.py", line 17, in ? content = content.encode('gb2312','ignore') UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 88: ordinal not in range(128) shell returned 1 Hit any key to close this window... My python version is 2.4, Does it have some problems with asian encoding support? Thanks! On Dec 31 2006, 9:30 pm, "Felipe Almeida Lessa" <[EMAIL PROTECTED]> wrote: > On 31 Dec 2006 05:20:10 -0800, JTree <[EMAIL PROTECTED]> wrote: > > > def funUrlFetch(url): > > lambda url:urllib.urlopen(url).read()This function only creates a > > lambda function (that is not used or > assigned anywhere), nothing more, nothing less. Thus, it returns None > (sort of "void") no matter what is its argument. Probably you meant > something like > > def funUrlFetch(url): > return urllib.urlopen(url).read() > > or > > funUrlFetch = lambda url:urllib.urlopen(url).read() > > > objUrl = raw_input('Enter the Url:') > > content = funUrlFetch(objUrl)content gets assigned None. Try putting "print > > content" before the unicode line. > > > content = unicode(content,"gbk")This, equivalent to unicode(None, "gbk"), > > leads to > > > TypeError: coercing to Unicode: need string or buffer, NoneType foundNone's > > are not strings nor buffers, so unicode() complains. > > See ya, > > -- > Felipe. -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about unicode() function
Thanks everyone! Sorry for my ambiguous question. I changed the codes and now it works fine. JTree wrote: > Hi,all > I encountered a problem when using unicode() function to fetch a > webpage, I don't know why this happenned. > My codes and error messages are: > > > Code: > #!/usr/bin/python > #Filename: test.py > #Modified: 2006-12-31 > > import cPickle as p > import urllib > import htmllib > import re > import sys > > def funUrlFetch(url): > lambda url:urllib.urlopen(url).read() > > objUrl = raw_input('Enter the Url:') > content = funUrlFetch(objUrl) > content = unicode(content,"gbk") > print content > content.close() > > > error message: > > C:\WINDOWS\system32\cmd.exe /c python test.py > Enter the Url:http://www.msn.com > Traceback (most recent call last): > File "test.py", line 16, in ? > content = unicode(content,"gbk") > TypeError: coercing to Unicode: need string or buffer, NoneType found > shell returned 1 > Hit any key to close this window... > > Any suggestions would be appreciated! > > Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about unicode() function
hi, I just removed the unicode() method from my codes. As John Machin said, I had an wrong understanding of unicode and ascii. Paul Watson wrote: > JTree wrote: > > Thanks everyone! > > > > Sorry for my ambiguous question. > > I changed the codes and now it works fine. > > > > > > > > JTree wrote: > >> Hi,all > >> I encountered a problem when using unicode() function to fetch a > >> webpage, I don't know why this happenned. > >> My codes and error messages are: > >> > >> > >> Code: > >> #!/usr/bin/python > >> #Filename: test.py > >> #Modified: 2006-12-31 > >> > >> import cPickle as p > >> import urllib > >> import htmllib > >> import re > >> import sys > >> > >> def funUrlFetch(url): > >> lambda url:urllib.urlopen(url).read() > >> > >> objUrl = raw_input('Enter the Url:') > >> content = funUrlFetch(objUrl) > >> content = unicode(content,"gbk") > >> print content > >> content.close() > >> > >> > >> error message: > >> > >> C:\WINDOWS\system32\cmd.exe /c python test.py > >> Enter the Url:http://www.msn.com > >> Traceback (most recent call last): > >> File "test.py", line 16, in ? > >> content = unicode(content,"gbk") > >> TypeError: coercing to Unicode: need string or buffer, NoneType found > >> shell returned 1 > >> Hit any key to close this window... > >> > >> Any suggestions would be appreciated! > >> > >> Thanks! > > So... How about posting the brief working code? -- http://mail.python.org/mailman/listinfo/python-list