我想通过API从变量“数据”中保存JSON文件。
我已经创建了一个函数writetoJsonfile来执行此操作,但是它不起作用。
你们能帮我吗?谢谢。
导入请求,json,io 导入xmltodict 导入日志作为日志 将熊猫作为pd导入 从io import BytesIO 从zipfile导入ZipFile 从datetime导入datetime
def writeToJSONFile(path,filename,cs):
path = 'C:/Users/Skelaton/Desktop'
filename = 'compromised_systems'
ext = 'json'
filePathNameWExt = path + filename + ext
with open(filePathNameWExt, 'w') as fp :
json.dump(cs, fp)
def getdata(person_id):
log.info("Downloading all data from people {}".format(person_id))
payload = {'format' : 'csv'}
r = requests.get
("https://example.com/people/{}/reports/person".format(person_id),
auth=(api_key,''), proxies=proxy,params=payload)
if r.status_code == 200:
data = pd.read_csv(BytesIO(r.content),error_bad_lines=False)
data = data.to_json(orient="records")
data = json.loads(data)
return data
else:
log.error("Unable to download all data due to status code :
{}".format(r.status_code))
return False
答案 0 :(得分:1)
您的文件路径似乎不正确。 这有效:
path = 'C:/Users/Skelaton/Desktop/'
filename = 'compromised_systems'
ext = '.json'
filePathNameWExt = path + filename + ext
def write_to_file(filename, data):
with open(filename, 'w+') as fp:
fp.write(json.dumps(data))