Python社区  »  Python

试图将书分成章节的python错误

Richard Ewing • 1 周前 • 11 次点击  
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from urllib.request import urlopen

#Reading the text of novel from a website
huck_fin_url = 'http://www.gutenberg.org/files/76/76-0.txt'
df = urlopen(huck_fin_url)
huck_fin_text = df.read()
#print(huck_fin_text)
huck_fin_chapters = huck_fin_text.split('CHAPTER ')[1:]

误差

文件“/users/richxxxxx/documents/readbooks.py”,第19行,in huck_fin_chapters=huck_fin_text.split('章')[1:]

typeerror:需要一个bytes-like对象,而不是'str'

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40218
 
11 次点击  
分享到微博
文章 [ 2 ]  |  最新文章 1 周前
Aidan H hyrama
Reply   •   1 楼
Aidan H hyrama    6 月前
from urllib.request import urlopen

huck_fin_url = 'http://www.gutenberg.org/files/76/76-0.txt'  
df = urlopen(huck_fin_url)  
huck_fin_text = str(df.read())
huck_fin_chapters = huck_fin_text.split('CHAPTER ')[1:]  
print(huck_fin_chapters)

你必须在df.read()前面添加str

rgk
Reply   •   2 楼
rgk    6 月前

urlopen 返回字节流而不是字符串,并且 .split() 对那些对象不可用。您需要首先根据正确的字符集对其进行解码:

from urllib.request import urlopen

#Reading the text of novel from a website
huck_fin_url = 'http://www.gutenberg.org/files/76/76-0.txt'
df = urlopen(huck_fin_url)
huck_fin_text = df.read().decode("utf8")
#print(huck_fin_text)
huck_fin_chapters = huck_fin_text.split('CHAPTER ')[1:]