我创建了一个像这样的对象:
company1.name = 'banana'
company1.value = 40
Run Code Online (Sandbox Code Playgroud)
我想保存这个对象.我怎样才能做到这一点?
我正在尝试使用pickle模块保存和加载对象.
首先,我声明我的对象:
>>> class Fruits:pass
...
>>> banana = Fruits()
>>> banana.color = 'yellow'
>>> banana.value = 30
Run Code Online (Sandbox Code Playgroud)
之后我打开一个名为'Fruits.obj'的文件(之前我创建了一个新的.txt文件,我重命名为'Fruits.obj'):
>>> import pickle
>>> filehandler = open(b"Fruits.obj","wb")
>>> pickle.dump(banana,filehandler)
Run Code Online (Sandbox Code Playgroud)
执行此操作后,我关闭了会话,然后我开始了一个新的会话,然后我将下一个(尝试访问它应该保存的对象):
file = open("Fruits.obj",'r')
object_file = pickle.load(file)
Run Code Online (Sandbox Code Playgroud)
但我有这样的信息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
ValueError: read() from the underlying stream did notreturn bytes
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么办,因为我不明白这个消息.有谁知道如何加载我的对象'香蕉'?谢谢!
编辑: 因为你们有些人已经吮吸我放:
>>> import pickle
>>> file = open("Fruits.obj",'rb')
Run Code Online (Sandbox Code Playgroud)
没有问题,但我接下来的是:
>>> object_file = …Run Code Online (Sandbox Code Playgroud) 您好我正在尝试使用下一段代码:
import pickle
object = Object()
filehandler = open(filename, 'w')
pickle.dump(object, filehandler)
Run Code Online (Sandbox Code Playgroud)
我想知道文件'filename'的扩展名是什么.谢谢!
我在Matlab中编写一个函数.应该有些不对劲.我的函数的输出是一个向量.当我看到我的矢量的每个元素时,我看到NaN(不是数字)而且NaNi,我真的不知道?
有谁知道是什么 NaNi?
我正在用Perl编写我的第一个程序,并写道:
use strict;
use warnings;
$animal = "camel";
print($animal);
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我从Windows命令行获取这些消息:
Global symbol "animal" requires explicit package name at stringanimal.pl line 3
Global symbol "animal" requires explicit package name at stringanimal.pl line 4
Run Code Online (Sandbox Code Playgroud)
请问,这些消息是什么意思?
我想知道如何保存我之前创建的二叉树.有谁知道怎么做?非常感谢.
PD:这里有一个关于如何实现二叉树的链接,我正在使用这个pice od代码:http: //code.activestate.com/recipes/286239-binary-ordered-tree/
我不明白使用sprintf命令发生的下一件事.
>> vpa(exp(1),53)
ans =
2.7182818284590455348848081484902650117874145507812500
>> e = 2.7182818284590455348848081484902650117874145507812500
e =
2.7183
>> sprintf('%0.53f', e)
ans =
2.71828182845904550000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)
为什么sprintf要向我显示数字e而不是数字,我保留在第一位?
我正在尝试使用VHDL语言制作ROM,我使用的是我在http://www.edaboard.com/thread38052.html上找到的模板:
library ieee;
use ieee.std_logic_1164.all;
entity ROM is
port ( address : in std_logic_vector(3 downto 0);
data : out std_logic_vector(7 downto 0) );
end entity ROM;
architecture behavioral of ROM is
type mem is array ( 0 to 2**4 - 1) of std_logic_vector(7 downto 0);
constant my_Rom : mem := (
0 => "00000000",
1 => "00000001",
2 => "00000010",
3 => "00000011",
4 => "00000100",
5 => "11110000",
6 => "11110000",
7 => "11110000",
8 => "11110000", …Run Code Online (Sandbox Code Playgroud) 我想在STD_LOGIC_VECTOR类型的变量中输入一个数字,但我的编译器有问题.
signal cl_output_ChA : STD_LOGIC_VECTOR (16-1 downto 0);
cl_ouput_ChA <= 111111111111111;
Run Code Online (Sandbox Code Playgroud)
编译器给我这两条消息:
谁能给我一个合适的代码行来把这个变量放入一个特定的数值?非常感谢.