python处理接⼝返回数据并导出Excel格式⽂件
背景:项⽬做了重构,需要对原有功能的数据进⾏处理和对⽐,这就需要被需要的字段获取到Excel⾥,通过筛选条件直观的看出哪些功能正常,
哪些功能不正常。
# -*- coding: UTF-8 -*-
import json
import requests
write的返回值import sys
import xlrd
from openpyxl import load_workbook
py import copy
reload(sys)
sys.setdefaultencoding('utf8')
url = "xxx/v1.0/schools/withServices?$count=true&$limit=200&$offset=0&_r=1600223532645&$filter=opened+eq+1&opened=1"
payload = "{}"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer "F4138BF83405E3B1B01A521337415C97A0DFDE23B417615AC35A650E0DAEFCED66CC11D183F04A25DE25ED31AD03BEAA055DE11107 'sdp-app-id': '144fa1cf-b8c7-43b2-b88f-a37005e6f6c7',
'userId': '2138822989'
}
response = quest("GET", url, headers=headers, data=payload)
ncode('utf8'))
res = json.)
list = []
for d in res["items"]:
# project.append(d["name"])
# project.append(d["org_id"])
# project.append(d["opened_num"])
project = {}
project["org_id"] = d["org_id"]
project["opened_num"] = d["opened_num"]
project["name"] = d["name"]
list.append(project)
#导出Excel格式的⽂件
#⽅法⼀:
# 解决乱码问题
print json.dumps(list, encoding="UTF-8", ensure_ascii=False)
param = json.dumps(project, encoding="UTF-8", ensure_ascii=False)
wb = load_workbook(r"D:\workplace\batchGetData\test.xlsx")
sheet = wb["Sheet1"]
start_row_num = 1
index = 0
# 把数值填充到excel表格中
for one in list:
index += 1
column_index = 1
for key, value in one.items():
column_index = column_index + 1
wb.save(r"D:\workplace\batchGetData\test1.xlsx")
#⽅法⼆:
# param = json.dumps(list, encoding="UTF-8", ensure_ascii=False)
# param = eval(param)
# f = open(r"D:\workplace\batchGetData\response1.json", "w+")
# json.dump(param, f, encoding="utf-8", ensure_ascii=False, indent=3)
# f.close()
# with open(r"D:\workplace\batchGetData\response1.json", "r") as fp:
# values = json.load(fp, encoding="utf-8")
# workbook = xlrd.open_workbook(r"D:\workplace\batchGetData\book1.xlsx")
# newWorkbook = copy(workbook)
# sheet_write = _sheet(0)
# sheet_write.write(0, 0, "name")
# sheet_write.write(0, 1, "org_id")
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论