社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

rgk

rgk 最近创建的主题
rgk 最近回复了
6 年前
回复了 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:]