Py学习  »  Python

在python中为测试目的用不同字符串重放文件

mario • 5 年前 • 1614 次点击  

我有以下文件,它调用一个端点并将结果保存到json文件中,以便对我的算法执行基准测试。

import requests
import json
import simplejson

r =     requests.get('http://localhost:5000/get_jobs/accurate,detail%20oriented,n     mbers,finance,analyst,optimistic,emotional%20intelligence,positive,calm,resilient,stable,committed,competitive,ambitious,determined,targets,goal-oriented,quick%20learner').json()

f = open('ProfileA.json', 'w')
simplejson.dump({'this is a test on Profile A with the following words: accurate,detail%20oriented,numbers,finance,analyst,optimistic,emotional%20intelligence,positive,calm,resilient,stable,committed,competitive,ambitious,determined,targets,goal-oriented,quick%20learner' : r}, f,sort_keys = True, indent = 4)
f.close()

我想知道python中是否有任何方法可以简单地用随机字符串生成器复制文件。

我在想办法,比如说一个正则表达式,它可以提取字符串,如果它发现这个字符串会用一个新的字符串写入另一个文件,但是听起来效率很低。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43189
 
1614 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Alpha
Reply   •   1 楼
Alpha    6 年前

可以使用哈希将内容映射到固定长度的字符串:

import hashlib

hashlib.sha256(json.dumps(info).encode('utf-8')).hexdigest()

在哪里? info 你的东西也是 dict , str ,…