python "AttributeError: 'str' object has no attribute 'decode'"错误
代码如下:
=================================代码==========================
import re
f1=open('J:/wenjian/1/1.txt','r').read()#把整个文件读进来作为整个字符串
f1.decode()#把byte类型转换成string类型
f1=re.sub('abcdef','123456',f1)#替换所有符合的情况
print (f1)#此时已经替换成功
f_w=open('J:/wenjian/1/replace_1.txt','wb')#新建一个文件,把替换后的内容写进去
f1=f1.encode('utf-8')#从string转换成bytes类型
f_w.write(f1)#必须要是bytes类型才能写进去
f_w.close()
======================代码======================================
报错如下:
Traceback (most recent call last):
File "J:\wenjian\python\test.py", line 8, in <module>
f1.decode()#把byte类型转换成string类型
AttributeError: 'str' object has no attribute 'decode'
>>>
系统是win7*64 python 3.3.1版本。
望大侠帮助。真诚感谢。
=================================代码==========================
import re
f1=open('J:/wenjian/1/1.txt','r').read()#把整个文件读进来作为整个字符串
f1.decode()#把byte类型转换成string类型
f1=re.sub('abcdef','123456',f1)#替换所有符合的情况
print (f1)#此时已经替换成功
f_w=open('J:/wenjian/1/replace_1.txt','wb')#新建一个文件,把替换后的内容写进去
f1=f1.encode('utf-8')#从string转换成bytes类型
f_w.write(f1)#必须要是bytes类型才能写进去
f_w.close()
======================代码======================================
报错如下:
Traceback (most recent call last):
File "J:\wenjian\python\test.py", line 8, in <module>
f1.decode()#把byte类型转换成string类型
AttributeError: 'str' object has no attribute 'decode'
>>>
系统是win7*64 python 3.3.1版本。
望大侠帮助。真诚感谢。
...全文
相关推荐
详解Python中的编码问题(encoding与decode、str与bytes)
- `AttributeError: ‘URLError’ object has no attribute ‘code’`:通常出现在网络请求中,当URL请求失败时,抛出`URLError`异常,而该异常对象并没有
Python3中bytes类型转换为str类型
"Python3中处理字符串和二进制数据时,str和bytes类型的区分是关键。当遇到需要将bytes类型转换为str类型时,有以下两种常见方法:使用str函数和利用bytes.decode()方法
python将unicode和str互相转化的实现
# 使用decode方法进行转换s_unicode = s_str.encode('unicode-escape').decode('unicode-escape')print(s_unicode) #
解决python3 urllib中urlopen报错的问题
"<stdin>", line 1, in <module>AttributeError: 'module' object has no attribute 'urlopen'```这个错误表明Python
python2 中 unicode 和 str 之间的转换及与python3 str 的区别
Str 转换为 Unicode在 Python2 中,可以使用 `decode()` 方法将 `str` 类型转换为 `unicode` 类型:```pythons = "hello"u = s.decode


