【发布时间】:2021-11-09 11:31:09
【问题描述】:
当file 命令运行时,我有一个具有以下输出的文件:
#file test.bin
#test.bin : data
#file -i test.bin
#test.bin: application/octet-stream; charset=binary
我想读取这个文件的内容并转发到一个接受这个读取数据作为字符串的python库。
file = open("test.bin", "rb")
readBytes = file.read() # python type : <class 'bytes'>
output = test.process(readBytes) # process expects a string
我试过str(readBytes),但是没有用。我看到文件test.bin 中也有不可打印的字符串,因为strings test.bin 的输出产生的输出比文件中存在的实际字节少得多。
有没有办法将读取的字节转换为字符串?还是我试图实现一些毫无意义的事情?
【问题讨论】:
-
只是不要将其读取为字节。
file = open("test.bin", "r") -
@DavidMeu :我以前试过这个,它给了我以下错误。
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 18: invalid start byte。正在读取的文件是加密的密钥材料。我想读取文件数据并将其转发到 python 库。
标签: python string file character-encoding