【问题标题】:How to append new data to pickle file using python如何使用python将新数据附加到pickle文件
【发布时间】:2020-02-24 18:02:55
【问题描述】:

我正在提取图像的人脸嵌入并将其附加到现有的 pickle 文件中。但看起来它不起作用,因为当我解开文件时,它不包含添加的新数据。下面是代码:

file = client_dir + '\embeddings.pickle'
data = {"embeddings": known_embeddings, "names": known_names}
with open(file, 'ab+') as fp:
    pickle.dump(data, fp)
    fp.close()
log("[INFO] Data appended to embeddings.pickle ")

当前的pickle文件包含以下数据:

{'embeddings': [array([-0.03656099,  0.11354745, -0.00438912,  0.0367547 ,  0.06391761,
        0.18440282,  0.06150107, -0.17380905,  0.03094344, -0.00182147,
        0.00969766,  0.06890091,  0.04974053, -0.0502388 , -0.03414046,
       -0.13550822, -0.02251128,  0.14556041, -0.04045469,  0.06500552,
        0.0726142 , -0.04139924, -0.04662199,  0.08869533, -0.00061307,
       -0.11912274,  0.13141112, -0.00648551,  0.00296356,  0.03682912,
       -0.15076959,  0.03989822,  0.02799555,  0.03429572,  0.09865954,
        0.14113557, -0.08355764,  0.09193961, -0.00819231, -0.01184336,
       -0.12519744,  0.00668721,  0.0816237 ,  0.00464355, -0.00339399,
        0.07501812,  0.11679655, -0.09211859,  0.06211261, -0.00543289,
        0.10347278,  0.06651585, -0.01512023,  0.09477805,  0.09886038,
       -0.03837246,  0.02265131, -0.14867221,  0.00781244,  0.04845129,
       -0.0363168 , -0.00186919, -0.16163988,  0.09539618,  0.14983718,
        0.09159472, -0.05315595, -0.05073383,  0.01501674, -0.03789762,
        0.07116041,  0.07650694, -0.02975985], dtype=float32)], 'names': ['rock']} 

我要附加的新数据如下:

{'embeddings': [array([-0.03656099,  0.11354745, -0.00438912,  0.0367547 ,  0.06391761,
        0.18440282,  0.06150107, -0.17380905,  0.03094344, -0.00182147,
        0.00969766,  0.06890091,  0.04974053, -0.0502388 , -0.03414046,
        0.07501812,  0.11679655, -0.09211859,  0.06211261, -0.00543289,
        -0.13550822, -0.02251128,  0.14556041, -0.04045469,  0.06500552,
        0.0726142 , -0.04139924, -0.04662199,  0.08869533, -0.00061307,
       -0.11912274,  0.13141112, -0.00648551,  0.00296356,  0.03682912,
       -0.15076959,  0.03989822,  0.02799555,  0.03429572,  0.09865954,
        0.14113557, -0.08355764,  0.09193961, -0.00819231, -0.01184336,
       -0.12519744,  0.00668721,  0.0816237 ,  0.00464355, -0.00339399,
        0.10347278,  0.06651585, -0.01512023,  0.09477805,  0.09886038,
       -0.03837246,  0.02265131, -0.14867221,  0.00781244,  0.04845129,
       -0.0363168 , -0.00186919, -0.16163988,  0.09539618,  0.14983718,
        0.09159472, -0.05315595, -0.05073383,  0.01501674, -0.03789762,
        0.07116041,  0.07650694, -0.02975985], dtype=float32)], 'names': ['john']}

但是当我解开文件时,它只有rock 的数据,而不是john。谁能帮助我做错了什么。下面是我用来解压并观察添加了哪些数据的代码。可能是我取消文件的方式是错误的,因为当我附加数据时,我可以看到文件大小增加。

import pickle

file = open('G:\\output\\embeddings.pickle', 'rb')

data = pickle.load(file)

file.close()

print(data)

请帮忙。谢谢

更新代码:

file_path = client_dir + '\embeddings.pickle'
file = open(file_path, 'rb')
old_data = pickle.load(file)
new_embeddings = old_data['embeddings']
new_names = old_data['names']
new_embeddings.append(known_embeddings[0])
new_names.append(known_names[0])
data1 = {"embeddings": new_embeddings, "names": new_names}
with open(file_path, 'ab+') as fp:
    pickle.dump(data1, fp)
    fp.close()
log.error("[INFO] Data appended to embeddings.pickle ")

在上面的代码中,我首先将pickle文件中的数据加载到列表中,然后将新数据附加到列表中,然后将所有数据(旧+新)添加到pickle文件中。谁能告诉我这是否是正确的做法。

在此之后,当我解开文件时,我并没有获得所有数据。谢谢

【问题讨论】:

  • 如果你在同一个文件上多次调用dump,我认为pickle 协议不能保证工作。不要腌制一个字典然后另一个,尝试腌制一个包含这两个字典的列表。
  • @Kevin 您的意思是首先将所有泡菜数据加载到列表/字典中,然后将新数据附加到该列表/字典中,然后将其保存在泡菜文件中。?我认为这可以工作。我会试试的
  • @Kevin 我已经更新了代码,你可以检查一次。

标签: python file pickle


【解决方案1】:

无需先加载数据即可提高速度: 如果文件不存在则使用mode='ab' 创建一个新文件,如果文件存在则追加数据:

pickle.dump((data), open('data folder/' + filename2save + '.pkl', 'ab'))

【讨论】:

    【解决方案2】:
    file_path = client_dir + '\embeddings.pickle'
    file = open(file_path, 'rb')
    old_data = pickle.load(file)
    new_embeddings = old_data['embeddings']
    new_names = old_data['names']
    new_embeddings.append(known_embeddings[0])
    new_names.append(known_names[0])
    data1 = {"embeddings": new_embeddings, "names": new_names}
    with open(file_path, 'ab+') as fp:
        pickle.dump(data1, fp)
        fp.close()
    log.error("[INFO] Data appended to embeddings.pickle ")
    

    这对我来说看起来非常接近正确。您成功加载腌制数据并向其添加新元素。问题似乎是with open(file_path, 'ab+') as fp: 调用。如果您以“a”模式打开文件,那么您写入的 pickle 数据将被添加到最后,在旧的 pickle 数据之后。然后,在程序的后续执行中,pickle.load 将仅加载旧的 pickle 数据。

    尝试用新的 pickle 数据完全覆盖旧的 pickle 数据。您可以改为以“w”模式打开来做到这一点。

    with open(file_path, 'wb') as fp:
        pickle.dump(data1, fp)
    

    顺便说一句,您不需要fp.close() 电话。 with 语句会在块的末尾自动关闭打开的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 2018-02-22
      • 2017-11-17
      • 2021-10-01
      相关资源
      最近更新 更多