import pandas as pd import time import requests from pprint import pprint file_path = r'C:\Users\Administrator.DESKTOP-7IC2USJ\Desktop\工单明细表test.xlsx' df = pd.read_excel(file_path) # 映射表格字段用途 field_type_mapping = { '度量': 'MEASURE', '维度': 'DIMENSION', '通用': 'null' } df['mapped_type'] = df['字段用途'].map(field_type_mapping) # 1.获取路径中的数据集位置 def DataNum(): url ="https://insight.f6car.com/api/apps/1000/datasets?offset=0&limit=30&orderBy=createdAt&orderType=desc&meta=false&functions=false&analytic=false&joinExcept=metrics%2Cconnection&requestId=01J9SZ6SZQ92SCQC6RHDMZ7W8X" cookies_str = '_USER_SESSION_ID=cfecc17c-ab5d-4d78-8aaa-bc4719ddab1f; csrf=5903f3e8-31df-4c1d-84d2-21e89c995b64' cookies_str = cookies_str.encode('utf-8').decode('latin-1') cookie_dict = {item.split('=')[0]: item.split('=')[1] for item in cookies_str.split('; ')} headers = { 'accept':'application/json', 'accept-encoding':'gzip, deflate, br, zstd', 'accept-language':'zh-CN', 'content-type':'application/json', 'priority':'u=1, i', 'referer':'https://insight.f6car.com/app/1000/dataset', 'sec-ch-ua':'"Chromium";v="129", "Not;A=Brand";v="8", "Google Chrome";v="129"', 'sec-ch-ua-mobile':'?0', 'sec-ch-ua-platform':'"Windows"', 'sec-fetch-dest':'empty', 'sec-fetch-mode':'cors', 'sec-fetch-site':'same-origin', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0', 'x-request-domain':'https://insight.f6car.com', 'x-request-hash-url':'https://insight.f6car.com/app/1000/dataset', 'x-request-proxy':'http://localhost:3003' } res = requests.get(url,cookies=cookie_dict,headers=headers) json = res.json() data = json.get("data") # 获取数据 titles_and_num = {} for item in data: if isinstance(item, dict): title = item.get('title') dataset = item["execDetail"]["jobParams"]["dataset"] if title and dataset: titles_and_num[title]=dataset # 将title与dataset对应存放到字典里 print(titles_and_num) # 将现有数据集打印出来 try: title_input = input("请输入数据集名字:") # 输入名字“:”是中文符号 dataset = titles_and_num.get(title_input) return(dataset) except: print("数据集不存在") # #2.设置参数 Data_number=DataNum() # 数据集(按创建时间依次类推) for index,row in df.iterrows():# 在列表格式固定 name = row["原始字段id"]# 需要修改的原始字段id new_message= row["需要修改的目标"]# 需要修改的目标 type = row["字段类型"] #字段类型 purpose =row["mapped_type"]# 字段用途:度量 MEASURE, 维度 DIMENSION,通用 null userComment = row["字段描述"] # 字段描述 null_format = row["字段描述"] # 字段描述 # 非常用变量 cookie = "csrf=756e767f-5bc6-4d2c-af6f-84142e4fe7fb; _USER_SESSION_ID=cfecc17c-ab5d-4d78-8aaa-bc4719ddab1f; pac4jCsrfToken=d06e1f960f12450080e6253a06971dc9" csrf_token="756e767f-5bc6-4d2c-af6f-84142e4fe7fb" requestId="01J9R2QGP1MGR7E3GVHS6W3G2R" headers = { 'accept':'application/json', 'accept-encoding':'gzip, deflate, br, zstd', 'accept-language':'zh-CN', 'content-length':'340', 'content-type':'application/json', 'origin':'https://insight.f6car.com', 'priority':'u=1, i', 'referer':f'https://insight.f6car.com/app/1000/dataset/{Data_number}/fields',#不同数据集需要修改 'cookie':f'{cookie}',#不同数据集内容相同 'sec-ch-ua':'"Chromium";v="129", "Not;A=Brand";v="8", "Google Chrome";v="129"', 'sec-ch-ua-mobile':'?0', 'sec-ch-ua-platform':'"Windows"', 'sec-fetch-dest':'empty', 'sec-fetch-mode':'cors', 'sec-fetch-site':'same-origin', 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0', 'x-csrf-token':f'{csrf_token}', 'x-request-domain':'https://insight.f6car.com', 'x-request-hash-url':f'https://insight.f6car.com/app/1000/dataset/{Data_number}/fields',# 不同数据集需要修改 'x-request-proxy':'http://localhost:3003' } # 修改数据方法 def change_message(): change_message_url = f"https://insight.f6car.com/api/apps/1000/datasets/{Data_number}/field?fieldName={name}&requestId={requestId}" # 修改数据请求的url地址 json1 = {"datasetId":f"{Data_number}", "fieldName":f"{name}", "purpose": f"{purpose}", "tags":{}, "isMetric":"false","visible":"true", "label":f"{new_message}", "type":f"{type}", "config":{"dialectName":"MysqlDialect", "formatter":{"number":{"prefix":"","suffix":"","percent":"","unit":"", "thousands":"false","decimal":2,# 保留小数点位数 "scientificNotation":"false", "fillInDecimalPlaces":"false","showPlus":"false","showSuffix":"true", "empty_format":{"type":null_format}, "null_format":{"type":null_format}, "nullReplace":{"kind":"constant","op":null_format,"type":"number"}}}}, "comment":"工单明细id", "originType":"integer", "hideValue":"false", "nativeType":"BIGINT UNSIGNED", "userComment":f"{userComment}", "detectedType":"integer", "suggestedTypes":["number","string","date"], "defaultAggrType":"sum", "basicType":"number", "groupName":"-"} res = requests.put(change_message_url,headers=headers,json=json1) return res.status_code # 添加字段方法 def add_key(): key_name = new_message # 新字段名称 function =row["函数方法"] #函数方法 add_key_url= f"https://insight.f6car.com/api/apps/1000/datasets/{Data_number}/fields?requestId={requestId}" data2 = {"options":{"label":f"{key_name}","visible":"true","expr":{"kind":"formula","op":f"{function}","value":f"{function}"}}} res = requests.post(add_key_url,headers=headers,json=data2) return res.status_code,key_name change_message = change_message() print(change_message) time.sleep(0.5) if change_message == 200: print("数据修改成功") else: if name: try: add_key,keyname = add_key() if add_key==200: print(keyname + "数据已添加") else: print(keyname +"数据添加失败") with open("D:\\输出文件\添加失败.csv",mode="a",encoding="utf-8") as f: f.write(keyname+"\n") except: continue else: continue