|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我把文件读取变成字节流文件,
存放到另一个新文件,
但当我读取新文件想将字节流还原成原来的内容使用decode方法不支持字符串,
也试过rb模式还是不行,求解决办法!
你学编程是为了干嘛;
字符串截取等等很简单吧。你可以加密的时候去掉,也可以解密后去掉。
实在不行我也可以再给你上个代码。
也不用你一问再问后面还有一个\n 是要一并去了对不对?
- file_path= '222.txt'
- read_file = open(file_path,'r')
- save_file = open(os.path.splitext(file_path)[0] + '.ep','w')
- for x in read_file :
- x = x.strip('\n') # 去除字符串最后的换行
- save_file.write(str(bytes(x,'utf-8'))[2:-1] + '\n') # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '
- read_file.close()
- save_file.close()
- file_path= '222.ep'
- read_file = open(file_path,'r')
- save_file = open(os.path.splitext(file_path)[0] + '.new','w')
- for x in read_file :
- temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
- temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
- save_file.write(temp)
- read_file.close()
- save_file.close()
复制代码
- file_path= '222.txt'
- 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')
- read_file.close()
- save_file.close()
- file_path= '222.ep'
- read_file = open(file_path,'r')
- save_file = open(os.path.splitext(file_path)[0] + '.new','w')
- for x in read_file :
- temp = eval(repr(x).replace('\\\\', '\\')) # 文本中的 \ 经读取后会变成 \\ 的处理
- temp = bytes(temp,'l1').decode('utf-8') # 包含字节的字符串解码
- temp = temp.strip('\n') # 去除字符串最后的换行
- temp = temp[2:-1] # 字符串切片方式[2:-1] 去掉字符串中的b' 和 '
- save_file.write(temp)
- read_file.close()
- save_file.close()
复制代码
|
|