社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Richard Ewing • 4 年前 • 157 次点击  
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
 
157 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Aidan H hyrama
Reply   •   1 楼
Aidan H hyrama    5 年前
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    5 年前

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