鱼C论坛

 找回密码
 立即注册
查看: 1273|回复: 9

[已解决]字符串不能使用decode()方法

[复制链接]
发表于 2019-1-8 22:37:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我把文件读取变成字节流文件,
存放到另一个新文件,
但当我读取新文件想将字节流还原成原来的内容使用decode方法不支持字符串,
也试过rb模式还是不行,求解决办法!
最佳答案
2019-1-9 21:02:02
jermey1994 发表于 2019-1-9 20:44
可以是可以了,不过,还是带有b' '

你学编程是为了干嘛;
字符串截取等等很简单吧。你可以加密的时候去掉,也可以解密后去掉。
实在不行我也可以再给你上个代码。
也不用你一问再问后面还有一个\n 是要一并去了对不对?

  1. file_path= '222.txt'

  2. read_file = open(file_path,'r')
  3. save_file = open(os.path.splitext(file_path)[0] + '.ep','w')

  4. for x in read_file :
  5.     x = x.strip('\n') # 去除字符串最后的换行
  6.     save_file.write(str(bytes(x,'utf-8'))[2:-1] + '\n')  # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '

  7. read_file.close()
  8. save_file.close()


  9. file_path= '222.ep'
  10. read_file = open(file_path,'r')
  11. save_file = open(os.path.splitext(file_path)[0] + '.new','w')
  12. for x in read_file :
  13.     temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
  14.     temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
  15.     save_file.write(temp)
  16. read_file.close()
  17. save_file.close()
复制代码


  1. file_path= '222.txt'

  2. read_file = open(file_path,'r')
  3. save_file = open(os.path.splitext(file_path)[0] + '.ep','w')

  4. for x in read_file :
  5.     save_file.write(str(bytes(x,'utf-8')) + '\n')  

  6. read_file.close()
  7. save_file.close()


  8. file_path= '222.ep'
  9. read_file = open(file_path,'r')
  10. save_file = open(os.path.splitext(file_path)[0] + '.new','w')
  11. for x in read_file :
  12.     temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
  13.     temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
  14.     temp = temp.strip('\n')  # 去除字符串最后的换行
  15.     temp = temp[2:-1] # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '
  16.     save_file.write(temp)
  17. read_file.close()
  18. save_file.close()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-1-9 21:02:02 | 显示全部楼层    本楼为最佳答案   
jermey1994 发表于 2019-1-9 20:44
可以是可以了,不过,还是带有b' '

你学编程是为了干嘛;
字符串截取等等很简单吧。你可以加密的时候去掉,也可以解密后去掉。
实在不行我也可以再给你上个代码。
也不用你一问再问后面还有一个\n 是要一并去了对不对?

  1. file_path= '222.txt'

  2. read_file = open(file_path,'r')
  3. save_file = open(os.path.splitext(file_path)[0] + '.ep','w')

  4. for x in read_file :
  5.     x = x.strip('\n') # 去除字符串最后的换行
  6.     save_file.write(str(bytes(x,'utf-8'))[2:-1] + '\n')  # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '

  7. read_file.close()
  8. save_file.close()


  9. file_path= '222.ep'
  10. read_file = open(file_path,'r')
  11. save_file = open(os.path.splitext(file_path)[0] + '.new','w')
  12. for x in read_file :
  13.     temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
  14.     temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
  15.     save_file.write(temp)
  16. read_file.close()
  17. save_file.close()
复制代码


  1. file_path= '222.txt'

  2. read_file = open(file_path,'r')
  3. save_file = open(os.path.splitext(file_path)[0] + '.ep','w')

  4. for x in read_file :
  5.     save_file.write(str(bytes(x,'utf-8')) + '\n')  

  6. read_file.close()
  7. save_file.close()


  8. file_path= '222.ep'
  9. read_file = open(file_path,'r')
  10. save_file = open(os.path.splitext(file_path)[0] + '.new','w')
  11. for x in read_file :
  12.     temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
  13.     temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
  14.     temp = temp.strip('\n')  # 去除字符串最后的换行
  15.     temp = temp[2:-1] # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '
  16.     save_file.write(temp)
  17. read_file.close()
  18. save_file.close()
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
jermey1994 + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-8 23:10:46 | 显示全部楼层
你怎么压就怎么解。

看不懂你就上代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-9 12:00:49 | 显示全部楼层
ba21 发表于 2019-1-8 23:10
你怎么压就怎么解。

看不懂你就上代码。

        read_file = open(file_path,'r')
        save_file = open(os.path.splitext(file_path)[0] + '.ep','w')
        for x in read_file :
            save_file.write(str(bytes(x,'utf-8')) + '\n')  
        eg.msgbox('文件加密完成')
        read_file.close()
        save_file.close()

read_file = open(file_path,'r')
        save_file = open(os.path.splitext(file_path)[0] + '.new','w')
        for x in read_file :
            temp = x
            save_file.write(temp.decode())
        read_file.close()
        save_file.close()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-9 12:03:06 | 显示全部楼层
ba21 发表于 2019-1-8 23:10
你怎么压就怎么解。

看不懂你就上代码。

但是新文件重新读取时是返回字符串的,而字符串用不了decode()方法
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-9 18:26:43 | 显示全部楼层
jermey1994 发表于 2019-1-9 12:03
但是新文件重新读取时是返回字符串的,而字符串用不了decode()方法
  1. read_file = open(file_path,'r')
  2. save_file = open(os.path.splitext(file_path)[0] + '.new','w')
  3. for x in read_file :
  4.     temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
  5.     temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
  6.     save_file.write(temp)
  7. read_file.close()
  8. save_file.close()
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-9 20:44:25 | 显示全部楼层

可以是可以了,不过,还是带有b' '
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-9 21:19:12 | 显示全部楼层
ba21 发表于 2019-1-9 21:02
你学编程是为了干嘛;
字符串截取等等很简单吧。你可以加密的时候去掉,也可以解密后去掉。
实在不行我 ...

好的,谢谢大佬...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-9 21:23:32 | 显示全部楼层

请结贴
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-11 17:37:23 | 显示全部楼层
2019111_173654.png
结贴
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 18:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表