【发布时间】:2017-09-27 21:41:03
【问题描述】:
我正在使用 python 3,但不明白为什么输出的每个行首都有 b。我认为python 2不是这种情况。为什么会这样以及如何删除它?谢谢
import urllib
# fhand = urllib.urlopen('http://www.py4inf.com/code/romeo.txt') in Python 2
fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
for line in fhand:
print(line.strip())
输出看起来像这样
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'
【问题讨论】:
-
这个答案应该有助于阐明这一点。基本上,字符串是字节文字,在 Python 2 中被忽略。stackoverflow.com/questions/6269765/…
-
@AgnosticDev,谢谢!
-
没问题!
标签: python-3.x request urllib urlopen