用python并行加密json文件

2024-06-02 16:00:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json文件,它的记录就像

  "emp_no" : 10002,
    "birth_date" : "1964-06-02",
    "first_name" : "Bezalel",
    "last_name" : "Simmel",
    "gender" : "F",
    "hire_date" : "1985-11-21"

现在我想用OPE加密emp\u no! 我想同时做这件事,所以我试着

from multiprocessing import Pool
from contextlib import closing
import json
from ope import OPE

r_k = OPE.generate_key()
cipher = OPE(r_k)
f = open('/home/carol/test.json', 'r')
data = json.load(f)

def ope_enc(x):
    x['emp_no'] = cipher.encrypt(x['emp_no'])
    return x

if __name__ == '__main__':
    data = my json file
    with closing(Pool(processes=5)) as pool:
        result = pool.map(ope_enc, data)
    h = open('/home/carol/res.json', 'w')

    json.dump(result, h)

但它不起作用! 有人能帮我吗?! 谢谢。。。你知道吗


Tags: nonamefromimportjsonhomedatadate
2条回答

下面的代码(使用伪加密)

from multiprocessing import Pool
from contextlib import closing
import json

data = [{"emp_no": 10002,
        "birth_date": "1964-06-02",
        "first_name": "Bezalel",
        "last_name": "Simmel",
        "gender": "F",
        "hire_date": "1985-11-21"}, {"emp_no": 100044,
                                     "birth_date": "1964-06-02",
                                     "first_name": "Bezalel",
                                     "last_name": "Simmel",
                                     "gender": "F",
                                     "hire_date": "1985-11-21"}]


def ope_enc(x):
    # Just add 1
    x['emp_no'] = x['emp_no'] + 1
    return x


if __name__ == '__main__':
    with closing(Pool(processes=5)) as pool:
        result = pool.map(ope_enc, data)
    with open('res.json', 'w') as out:
        json.dump(result, out)

你知道吗res.json文件你知道吗

 [
   {
     "emp_no": 10003,
     "birth_date": "1964-06-02",
     "first_name": "Bezalel",
     "last_name": "Simmel",
      "gender": "F",
      "hire_date": "1985-11-21"
  },
  {
      "emp_no": 100045,
      "birth_date": "1964-06-02",
      "first_name": "Bezalel",
      "last_name": "Simmel",
       "gender": "F",
       "hire_date": "1985-11-21"
  }
]

closing不应该用在这种情况下,我想你的意思是:

    with Pool(5) as pool:
        result = pool.map(ope_enc, data)

相关问题 更多 >