Why does the 'str' object lack the 'decode' attribute error occur?
- 内容介绍
- 文章标签
- 相关推荐
本文共计494个文字,预计阅读时间需要2分钟。
问题:如何改写伪原创以下代码段,不使用缩写,不超过100字?
原始代码:pythondef get_ch_label(txt_file): labels= with open(txt_file, 'rb') as f: for label in f: labels=labels + label.decode('UTF-8') return labels
改写后:python定义函数get_ch_label接收txt_file参数,初始化labels字符串变量为空,使用with语句以二进制读取模式打开txt_file文件,遍历文件中的每一行label,使用UTF-8解码后,将其添加到labels字符串中,并返回解码后的labels字符串。
问题
先上代码:
def get_ch_lable(txt_file):labels= ""
with open(txt_file, 'rb') as f:
for label in f:
labels =labels+label.decode('UTF-8')
return labels
想细致研究下decode方法。
本文共计494个文字,预计阅读时间需要2分钟。
问题:如何改写伪原创以下代码段,不使用缩写,不超过100字?
原始代码:pythondef get_ch_label(txt_file): labels= with open(txt_file, 'rb') as f: for label in f: labels=labels + label.decode('UTF-8') return labels
改写后:python定义函数get_ch_label接收txt_file参数,初始化labels字符串变量为空,使用with语句以二进制读取模式打开txt_file文件,遍历文件中的每一行label,使用UTF-8解码后,将其添加到labels字符串中,并返回解码后的labels字符串。
问题
先上代码:
def get_ch_lable(txt_file):labels= ""
with open(txt_file, 'rb') as f:
for label in f:
labels =labels+label.decode('UTF-8')
return labels
想细致研究下decode方法。

