gpt4 book ai didi

python 2.5编码

转载 作者:行者123 更新时间:2023-11-28 20:50:35 25 4
gpt4 key购买 nike

# -*- coding: utf-8 -*-
from pyquery import PyQuery as pq
from urllib import urlencode
from urllib2 import Request,urlopen
def sendRequest(url, data = None, headersOnly = False):
headers = { 'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US;)' }
request = Request(url, data, headers)
return urlopen(request).read()

resp = sendRequest("https://foursquare.com/v/rivers-edge-cafe-- morrison/4c1907776e02b7132eae627b")
print pq(resp)("#venueCategories").text()

输出应该是 Café、Burger Joint、Sandwich Place但有异常(exception):

Traceback (most recent call last):
File "unicodeerr1.py", line 11, in <module>
print pq(resp)("#venueCategories").text()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)

最佳答案

您需要为您的源代码指定编码,或改用字符转义。

# -*- coding: utf-8 -*-

print 'caf\xc3\xa9' # UTF-8 representation of e accent egu.

虽然你可能想使用 Unicode 文字(这里有一个 unicode 转义字符):

print u'caf\u00e9'

请务必阅读 Unicode HOWTO以充分了解这里发生的事情。其他有用的文档:

请注意,您的特定错误与 python 2.5 和 2.7 没有任何关系,但与您要打印的任何内容的输出编码有关。。在使用 python2.5 的服务器上,没有指定编码或明确设置为 ASCII,但在使用 python 2.7 的本地计算机上,您很可能正在处理支持 UTF-8 的终端。

关于 python 2.5编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com