import requests
import json
import xlrd
if __name__ == '__main__':
#文件的全路径地址
file_contents = '/*/*/*/123.xlsx'
#将文件内容赋值给file对象
file = xlrd.open_workbook(file_contents)
#洗数据用的接口
url = 'http://*/*/*/*/*/*/update'
#索引从0开始,即从列的列名开始获取
#如果不需要列名,也可以从数据开始,即file.sheet_by_index(1)从1开始
#file.sheet_by_index()的入参为行号
sheet = file.sheet_by_index(0)
#定义一个二维数组,用来存储表格里的数据
#格式如下
#[
# [1,"模拟","/com/cn"]
# [2,"模拟2","/com/cn"]
# [3,"模拟3","/com/cn"]
#]
cls = []
#将数据填充到二维数组中
for i in range(sheet.nrows):
cls.append(sheet.row_values(i))
#循环洗入
#数组的索引是从0开始的,cls[1:]意思是从第2组数据开始
#这里之所以从第二组数据开始是因为第一组数据存的是表格的列名,我不需要列名
for cl in cls[1:]:
#给第一个变量赋值
test1 = cl[0]
#给第二个变量赋值
test2 = cl[1]
#拼接字典数据
data = {
"test1": test1,
"test2": test2
}
#转成json
#这里需要导包,import json
#倒错包会导致调用json.dumps(data)时,会将数据中的斜杆进行转义,不如将地址中的斜杠转义
print(json.dumps(data))
#调用接口,实现循环清洗
print(requests.post(url, json.dumps(data)))
python-使用open_workbook读取xlsx文件并调用接口洗数据
最新推荐文章于 2024-04-29 21:50:55 发布