python - Python 3.6:不能将 str 连接到字节
问题描述
我在读取 python 3.6.3 中的文本文件时遇到问题。
代码如下:
def read_docs(doc):
files=glob.glob(doc)
for var in files:
with open(var,'r', encoding='utf-8') as c1:
a1=reader(c1,'ignore').read()
我收到以下错误:
TypeError:无法将 str 连接到字节。
我见过类似的问题,但我不清楚如何处理它。我被卡住了,因为我无法在不阅读文本文件的情况下继续使用该应用程序。
任何建议表示赞赏。
提前致谢。
解决方案
尝试将文件模式更改为"rb"
这样open(var, 'rb', encoding='utf-8')
。文件模式"rb"
返回字节。文件模式"r"
返回字符串。
在此处查看有关此功能的文档:https ://docs.python.org/3.6/library/functions.html#open
推荐阅读
- javascript - Javascript“多维数组”(即数组的数组)的奇怪问题
- javascript - 光滑的多个类相同的功能 - 寻找最佳解决方案
- javascript - 渲染threeJs渲染器列表的问题
- c# - 在 ASP.NET Core 中使用证书身份验证调用 WCF 服务,而不使用传输安全模式
- c++ - 图片上的文字
- json - 如何在 VSCode 设置中设置 Prettier 以按文件类型有条件地应用单引号?
- android - 无法在 Kotlin 中使用 Retrofit TypeAdapter 将 Gson Doubles 舍入为 Longs
- java - 是否可以在 Spring Boot 中停止打印 AmqpRejectAndDontRequeueException 堆栈跟踪日志?
- python - 将元组出现转换为数据框列中的字符串
- mysql - 无法连接本地数据库。Sequel Pro 遇到意外错误