【发布时间】:2021-01-06 10:29:30
【问题描述】:
我正在调用 API URL 并将输出存储在 Csv 文件中,如下所示,由于 URL 在服务器上运行,它将 csv 文件存储在服务器中,我希望在 Rundeck 中运行时将其下载到桌面本地
代码:
import re
import json
import warnings
import urllib.request
import csv
warnings.filterwarnings('ignore', message='Unverified HTTPS request')
url = "http://lappmachine.server:4450/api/35/project/ProjectName01/executions"
headers = {
'Accept': 'application/json',
'X-Rundeck-Auth-Token': '#Tokens here#'
}
response = requests.request("GET", url, headers=headers, verify = False)
#print(response.text.encode('utf8'))
response_value = response.json()
response_value = json.dumps(response_value)
resp = json.loads(response_value)
with open('execute.csv','w') as executeData:
i = resp['executions']
res_list = []
with open('ExecOutput.csv','w') as writer:
for a in i:
try:
if (a['id']==0):
print("Is empty")
res_list.append('')
else:
print("Job ID is : " + str(a['id']))
res_list.append(str(a['id']))
except KeyError:
print("Job ID: Key error issue, Check input again")
try:
if(a['project']==0):
print("Project data not available")
res_list.append('')
else:
print("Project Name: "+a['project'])
res_list.append(str(a['project']))
except KeyError:
print("Project Name: Key error issue, Check input again")
try:
if(a['name']):
print("Job Name not available")
res_list.append('')
else:
print("JobName: "+a['name'])
res_list.append(str(str(a['name'])))
except KeyError:
print("JobName: key Error check again")
writer.write(','.join(res_list) + '\n')
而且 Csv 中的数据在一行中,我希望这一行低于另一行。
在 Csv 中预期:
【问题讨论】: