简体   繁体   中英

Can Python Pickle conserve object and variable types when dumping lists?

I am currently connecting to a remote database and querying a long list of items. I don't know how many items the database has total, but I have been querying about 3000 items that fit various parameters I search for. Once I get the item ID's I need, I then query the database for information on each item, the types are below :

[str, str, datetime, str, str, int, int, int, str, str, bool]

The problem is, it takes almost 45 minutes to request all of this information because the database is extremely large. To solve this, I'm trying to Pickle the list so that I can skip the query step for now while I'm trying to test and debug the other 90% of my code. Most of the rest of my code does operations on this list, but I need the list in order to test those operations.

I believe it's possible to just type cast each item field and then drop it back into my main list, but this seems really inefficient. Below is the code that deals with the collecting/pickling, along with an example operation I have to perform.

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]

Is there a way to have Pickle dump the list format as well so that when I pickle.load() I will get my list typed the same way as when I pickle.dump() ed it?

Pickle normally loads whatever you dumped in, including type information. For example:

泡菜用法示例

So I'm not sure what your problem is, I expect it depends on what you are actually dumping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM