python "AttributeError: 'str' object has no attribute 'decode'"错误

littleant1111 littleant1111 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:
=================================代码==========================
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版本。
望大侠帮助。真诚感谢。
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
littleant1111
脚本语言 37,737 成员 34,208 内容