The python using excel format data parsed into json

The python using excel format data parsed into json

Easily converted into json post request by pushing data items custom data, but also to facilitate testing;

to xlrd Import 
Import JSON
Import Requests

DEF OpenWorkbook ():
# table data read excel
Workbook = xlrd.open_workbook (r'D: \ data.xlsx ')
# select one that need to read data
sheet = workbook.sheet_by_index ( 0)
# number of rows and columns is obtained
rows = sheet.nrows
cols = sheet.ncols
# create an array to store the data in the excel
P = []
for I in Range (. 1, rows):
D = {}
for J Range in (0, cols):
Q = '% S'% sheet.cell (0, J) .Value
D [Q] = sheet.cell (I, J) .Value
AP = []
for K, V in D .items ():
IF the isinstance (V, a float): # default values excel in the float, determining the need for treatment, by ' "% s":% d ', ' "% s": "% s"'Array Format
ap.append('"%s":%d' % (k, v))
else:
ap.append('"%s":"%s"' % (k, v))
s = '{%s}' % (','.join(ap)) # 继续格式化
p.append(s)
t = '[%s]' % (','.join(p)) # 格式化
data=json.dumps(t,ensure_ascii=False)
print(data.replace("\\",""))
# with open('student4.json',"w",encoding='utf-8') as f:
# f.write(t)
#openWorkbook()
url="http://111.111.111.111:8000/pushdata/"
headers={"Content-Type":"application/json"}
data=openWorkbook()
re=requests.post(url=url,headers=headers,json=data)
print(re.text)

 

I wish the great motherland Happy Holidays!

Guess you like

Origin www.cnblogs.com/zjx012/p/11615719.html