Ogr*_*agi 2 urllib request urlopen python-3.x
我正在使用 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())
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样
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'
Run Code Online (Sandbox Code Playgroud)
小智 5
我知道这个问题一年多前就已经回答过,我发现一个简单的。我只是decode()在我的代码行中添加了 a 。
def fetch_words():
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode('utf-8').split()
for word in line_words:
story_words.append(word)
for word in story_words:
print(word)
Run Code Online (Sandbox Code Playgroud)
这有效并从每行的开头删除了 b 。
| 归档时间: |
|
| 查看次数: |
3264 次 |
| 最近记录: |