gpt4 book ai didi

Python 2to3 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 15:24:27 25 4
gpt4 key购买 nike

我目前正在接受 python 挑战,我已经达到 4 级,see here我只学习了几个月的 python,到目前为止,我一直在尝试通过 2.x 学习 python 3,除非我使用这段代码,这里是 python 2.x 版本:

import urllib, re
prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
findnothing = re.compile(r"nothing is (\d+)").search
nothing = '12345'
while True:
text = urllib.urlopen(prefix + nothing).read()
print text
match = findnothing(text)
if match:
nothing = match.group(1)
print " going to", nothing
else:
break

所以要将其转换为 3,我将更改为:

import urllib.request, urllib.parse, urllib.error, re
prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
findnothing = re.compile(r"nothing is (\d+)").search
nothing = '12345'
while True:
text = urllib.request.urlopen(prefix + nothing).read()
print(text)
match = findnothing(text)
if match:
nothing = match.group(1)
print(" going to", nothing)
else:
break

所以如果我运行 2.x 版本它工作正常,通过循环,抓取 url 并转到最后,我得到以下输出:

and the next nothing is 72198
going to 72198
and the next nothing is 80992
going to 80992
and the next nothing is 8880
going to 8880 etc

如果我运行 3.x 版本,我得到以下输出:

b'and the next nothing is 44827'
Traceback (most recent call last):
File "C:\Python32\lvl4.py", line 26, in <module>
match = findnothing(b"text")
TypeError: can't use a string pattern on a bytes-like object

因此,如果我将这一行中的 r 更改为 a b

findnothing = re.compile(b"nothing is (\d+)").search

我得到:

b'and the next nothing is 44827'
going to b'44827'
Traceback (most recent call last):
File "C:\Python32\lvl4.py", line 24, in <module>
text = urllib.request.urlopen(prefix + nothing).read()
TypeError: Can't convert 'bytes' object to str implicitly

有什么想法吗?

我是编程新手,所以请不要咬我的头。

_bk201

最佳答案

您不能隐式混合 bytes 和 str 对象。

最简单的方法是解码 urlopen().read() 返回的字节并在任何地方使用 str 对象:

text = urllib.request.urlopen(prefix + nothing).read().decode() #note: utf-8

该页面未通过 Content-Type 指定首选字符编码 header 或 <meta>元素。我不知道 text/html 的默认编码应该是什么但是rfc 2068 says :

When no explicit charset parameter is provided by the sender, mediasubtypes of the "text" type are defined to have a default charsetvalue of "ISO-8859-1" when received via HTTP.

关于Python 2to3 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9453206/

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