gpt4 book ai didi

python - 在 python 上快速加载文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:46 26 4
gpt4 key购买 nike

我在 python 上加载数据时遇到两个问题,这两个 scipts 都可以正常工作,但它们需要太多时间才能运行,有时结果是“Killed”(第一个)。

  1. 我有一个很大的压缩文本文件,我会这样做:

    import gzip
    import cPickle as pickle

    f = gzip.open('filename.gz','r')
    tab={}

    for line in f:
    #fill tab

    with open("data_dict.pkl","wb") as g:
    pickle.dump(tab,g)

    f.close()
  2. 我必须对我在之前的脚本中创建的字典进行一些操作

    import cPickle as pickle

    with open("data_dict.pkl", "rb") as f:
    tab = pickle.load(f)
    f.close()

    #operations on tab (the dictionary)

您还有其他解决方案吗?也许不是那些涉及 YAML 或 JSON 的......

最佳答案

如果你正在 pickle 的数据是原始和简单的,你可以试试 marshal 模块:http://docs.python.org/3/library/marshal.html#module-marshal .这就是 Python 用来序列化其字节码的方式,因此速度非常快。

关于python - 在 python 上快速加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20107551/

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