首页 > 解决方案 > 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 连接到字节。

我见过类似的问题,但我不清楚如何处理它。我被卡住了,因为我无法在不阅读文本文件的情况下继续使用该应用程序。

任何建议表示赞赏。

提前致谢。

标签: pythonpython-3.xtextpython-3.6

解决方案


尝试将文件模式更改为"rb"这样open(var, 'rb', encoding='utf-8')。文件模式"rb"返回字节。文件模式"r"返回字符串。

在此处查看有关此功能的文档:https ://docs.python.org/3.6/library/functions.html#open


推荐阅读