微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试将字节解码为字符串时出错

如何解决尝试将字节解码为字符串时出错

这是我获取二进制数据的函数

 PASS  examples/67099526/module2.test.js (11.508 s)
  67099526
    ✓ should initialize module1 correctly (8819 ms)
    ✓ should handle error (18 ms)

  console.log
    error

      at new Module2 (examples/67099526/module2.js:8:15)

------------|---------|----------|---------|---------|-------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
------------|---------|----------|---------|---------|-------------------
All files   |     100 |      100 |   33.33 |     100 |                   
 module2.js |     100 |      100 |   33.33 |     100 |                   
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed,1 total
Tests:       2 passed,2 total
Snapshots:   0 total
Time:        13.743 s

这是我返回的数据

def get_remote_file_binary_data(self,filename,connection_string = ""):
    file_data = None
    tmp_file_path = copyfile.make_temp_path()
    try:
        if connection_string:
            print("connection string is: ",connection_string) 
            source_file = copyfile.join(connection_string,filename)
        else:
            source_file = filename
    
        copyfile.copyfile(source_file,tmp_file_path)
    
        tmp_file = open(tmp_file_path,"rb")
        file_data = tmp_file.read()
        tmp_file.close()
    finally:
        os.remove(tmp_file_path)
    return file_data

这是调用上面函数代码

b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00
\x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01
\xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"

这是我的尝试以及我在评论中遇到的错误

zip_file = self.get_remote_file_binary_data(zip_filename,connection_str)

解决方法

IntStream src ="apple".chars();
        String a =src.mapToObj(s -> {
            if (s == 97) {
                return "aFbcr";
            } else if (s == 101) {
                return "eFbcr";
            }
            return String.valueOf((char) (s));
        }).collect(Collectors.joining());
        System.out.println(a);

输出:

import io
from zipfile import ZipFile

bites = b"PK\x03\x04\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00\x00\x00fake_xml.xml)\xe0H\x86]\x83\xd2\xb8\\xe2\x10\x95f\xde\xa6\x83|&\xa5\x99VwOS\x19\xea\xdb}\xee1[\xe0\x0f'0:C|6u2\xe4\xa6\xbcc\x8e\xdb\xd16(\x1d?\xba\x98Vp\x11\xb5\x06\xe8\xdcn\x0b\xc5I'B\x83#W\xac\xe5*+t\xa6\xa9\xd5\x85\x04\x86+{\xad\xe1\x8b{\xbf\x88L\xc4\xff\xa9=\x18\x9di\xa96Y$3\xfa\x98\x9c\x10\x99\n\x80PG\x9c\xd0GPK\x01\x02?\x00\x14\x00\x01\x00\x08\x00Fu\x87RR\xa6cpr\x00\x00\x00z\x00\x00\x00\x0c\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00fake_xml.xml\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\x89R~\x1a\xe6+\xd7\x01 \xe3\xf8\x1a\xe6+\xd7\x01T\xb5N\x1a\xe6+\xd7\x01PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00^\x00\x00\x00\x9c\x00\x00\x00\x00\x00"

print(ZipFile(io.BytesIO(bites)).filelist[0])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。