'str' 对象在 Python3 中没有属性 'decode'

编程入门 行业动态 更新时间:2024-10-21 19:30:49
本文介绍了'str' 对象在 Python3 中没有属性 'decode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 python 3.3.4 中的解码"方法有一些问题.这是我的代码:

I've some problem with "decode" method in python 3.3.4. This is my code:

for lines in open('file','r'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t')

但我无法解码此问题的行:

But I can't decode the line for this problem:

AttributeError: 'str' object has no attribute 'decode'

你有什么想法吗?谢谢

推荐答案

一个编码字符串,一个解码字节.

One encodes strings, and one decodes bytes.

您应该从文件中读取字节并对其进行解码:

You should read bytes from the file and decode them:

for lines in open('file','rb'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t')

幸运的是 open 有一个编码参数,这使得这很容易:

Luckily open has an encoding argument which makes this easy:

for decodedLine in open('file', 'r', encoding='ISO-8859-1'): line = decodedLine.split('\t')

更多推荐

'str' 对象在 Python3 中没有属性 'decode'

本文发布于:2023-10-27 11:19:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   str   decode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!