私信  •  关注

rgk

rgk 最近创建的主题
rgk 最近回复了
5 年前
回复了 rgk 创建的主题 » 试图将书分成章节的python错误

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:]