python 3的urllib [英] urllib for python 3

查看:26
本文介绍了python 3的urllib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python3中这段代码有问题:

This code in python3 is problematic:

import urllib.request
fhand=urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
print(fhand.read())

它的输出是:

b'But soft what light through yonder window breaks'
b'It is the east and Juliet is the sun'
b'Arise fair sun and kill the envious moon'
b'Who is already sick and pale with grief'

为什么我得到 b'...'?我该怎么做才能得到正确的回应?

Why did I get b'...'? What could I do to get the right response?

正确的文字应该是

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

推荐答案

b'...' 是一个 字节字符串:字节数组,不是真正的字符串.

The b'...' is a byte string: an array of bytes, not a real string.

转换为真实字符串,请使用

fhand.read().decode()

这使用默认编码UTF-8".对于 ASCII 编码,使用

This uses the default encoding 'UTF-8'. For ASCII encoding, use

fhand.read().decode("ASCII")

例如

这篇关于python 3的urllib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆