gpt4 book ai didi

python - Javascript 可以加载 Python pickle 转储的字符串但不能加载文件

转载 作者:行者123 更新时间:2023-12-05 05:09:05 25 4
gpt4 key购买 nike

请看下面的测试代码,我可以使用 jpickle 库来解码 Python pickle 转储的字符串。

(base):~/python 
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps('Hello Python!')
b'\x80\x03X\r\x00\x00\x00Hello Python!q\x00.'
>>>
(base)~$ node
> const jpickle = require('jpickle');
undefined
> jpickle.loads('\x80\x03X\r\x00\x00\x00Hello Python!q\x00.')
'Hello Python!'

当我尝试将 pickle 字符串写入文件时,我无法将其加载回 nodejs。我的错误是什么?谢谢!

python 代码:

import pickle
with open('test.dat', 'wb') as fout:
pickle.dump('Hello Python!', fout)

JS:

const fs = require('fs');
const jpickle = require('jpickle');
const binary = fs.readFileSync('test.dat');
const data = jpickle.loads(binary)
console.log(data)

错误信息:

node_modules/jpickle/lib/jpickle.js:341
throw "Unhandled opcode '" + opcode + "'";
^
Unhandled opcode '128'

package.json中的jpickle,需要从GitHub上安装,目前是最新版本:

    "jpickle": "git+https://github.com/jlaine/node-jpickle.git"
}

最佳答案

这似乎对我有用。感谢Tomalak用于输入。

const binary = fs.readFileSync('test.dat', "binary");

虽然这个解决方案似乎阻止了进程......

关于python - Javascript 可以加载 Python pickle 转储的字符串但不能加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com