Python Pickle 可以在转储列表时保存对象和变量类型吗?
Can Python Pickle conserve object and variable types when dumping lists?
我目前正在连接到远程数据库并查询一长串项目。我不知道数据库总共有多少项,但我一直在查询适合我搜索的各种参数的大约 3000 项。获得所需的商品 ID 后,我会查询数据库以获取有关每个商品的信息,类型如下:
[str, str, datetime, str, str, int, int, int, str, str, bool]
问题是,由于数据库非常大,请求所有这些信息几乎需要 45 分钟。为了解决这个问题,我尝试对列表进行 Pickle,以便在我尝试测试和调试其他 90% 的代码时暂时跳过查询步骤。我的其余大部分代码都在此列表上执行操作,但我需要该列表才能测试这些操作。
我相信可以只对每个项目字段进行类型转换,然后将其放回我的主列表中,但这似乎效率很低。下面是处理 collecting/pickling 的代码,以及我必须执行的示例操作。
query_object = QueryObject()
item_ids = query_object.get_ids()
print("\nFinished collecting the ID's for", len(item_ids), "items.")
answer = input("Do you wish to load information from the disk or the database? (d = disk, i = database)\n")
if answer == "d":
file = open('item_information.txt', 'rb')
items_list = pickle.load(file)
# pickle loads everything in as a string, so currently I just manually cast each item in the list
for item in items_list:
item = [str(item[0]), str(item[1]), str(item[2]), str(item[3]), int(item[4]), int(item[5]), int(item[6]),
int(item[7]), str(item[8]), str(item[9]), bool(item[10])]
file.close()
elif answer == "i":
file = open('item_information.txt', 'wb')
items_list = query_object.gather_item_list_data(item_ids)
pickle.dump(items_list, file)
file.close()
else:
print("Bad input.", answer)
sys.exit(1)
# remove all items that aren't usable
items_list[:] = [item for item in items_list if item[9] is True]
有没有办法让 Pickle 转储列表格式,这样当我 pickle.load()
时,我的列表输入方式与 pickle.dump()
编辑时相同?
Pickle 通常会加载您转储的任何内容,包括类型信息。例如:
所以我不确定你的问题是什么,我想这取决于你实际倾销的是什么。
我目前正在连接到远程数据库并查询一长串项目。我不知道数据库总共有多少项,但我一直在查询适合我搜索的各种参数的大约 3000 项。获得所需的商品 ID 后,我会查询数据库以获取有关每个商品的信息,类型如下:
[str, str, datetime, str, str, int, int, int, str, str, bool]
问题是,由于数据库非常大,请求所有这些信息几乎需要 45 分钟。为了解决这个问题,我尝试对列表进行 Pickle,以便在我尝试测试和调试其他 90% 的代码时暂时跳过查询步骤。我的其余大部分代码都在此列表上执行操作,但我需要该列表才能测试这些操作。
我相信可以只对每个项目字段进行类型转换,然后将其放回我的主列表中,但这似乎效率很低。下面是处理 collecting/pickling 的代码,以及我必须执行的示例操作。
query_object = QueryObject()
item_ids = query_object.get_ids()
print("\nFinished collecting the ID's for", len(item_ids), "items.")
answer = input("Do you wish to load information from the disk or the database? (d = disk, i = database)\n")
if answer == "d":
file = open('item_information.txt', 'rb')
items_list = pickle.load(file)
# pickle loads everything in as a string, so currently I just manually cast each item in the list
for item in items_list:
item = [str(item[0]), str(item[1]), str(item[2]), str(item[3]), int(item[4]), int(item[5]), int(item[6]),
int(item[7]), str(item[8]), str(item[9]), bool(item[10])]
file.close()
elif answer == "i":
file = open('item_information.txt', 'wb')
items_list = query_object.gather_item_list_data(item_ids)
pickle.dump(items_list, file)
file.close()
else:
print("Bad input.", answer)
sys.exit(1)
# remove all items that aren't usable
items_list[:] = [item for item in items_list if item[9] is True]
有没有办法让 Pickle 转储列表格式,这样当我 pickle.load()
时,我的列表输入方式与 pickle.dump()
编辑时相同?
Pickle 通常会加载您转储的任何内容,包括类型信息。例如:
所以我不确定你的问题是什么,我想这取决于你实际倾销的是什么。