我正在尝试创建一个python脚本,该脚本执行以下操作:
分析csv文件
通过rest api将csv文件发送到远程服务器
我的代码正在解析csv文件并将其转换为json对象。
但是,当它导入到远程服务器时,只会写入第一行。
问:我需要循环并为每一行发送一个单独的http请求吗?我觉得那太麻烦了。我的csv文件有10000多行,这将在每日cron上运行。
问题:如何设置此项以在单个请求中导入所有行?
#Requests package for python import requests
import csv
import json
import requests
#Parse CSV file and convert to JSON
f = open('example_import_csv.csv', 'rU')
reader = csv.DictReader(f, fieldnames = ("u_date","u_product","u_serial_number"))
out = json.dumps([row for row in reader])
#Print output confirms that the JSON is formatted properly
print(">JSON" , out)
#Set request parameters
url = 'xxxxx'
user = 'xxxxxx'
pwd = 'xxxxxx'
#Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
#data=out contains the JSON object
#Problem is only the first row is imported
response = requests.post(url, auth=(user, pwd), headers=headers ,data=out)
#Check for HTTP codes other than 200
if response.status_code != 200:
print('Status:', response.status_code, 'Headers:', response.headers)
exit()
#Decode the JSON response into a dictionary and use the data
print('Status:',response.status_code,'Headers:',response.headers,'Response:',response.json())
### OUTPUT
# >JSON [{"u_serial_number": "11", "u_product": "Apples", "u_date": "1/12/15"}, {"u_serial_number": "12", "u_product": "Pears", "u_date": "1/29/15"}, {"u_serial_number": "13", "u_product": "Oranges", "u_date": "1/12/15"}, {"u_serial_number": "14", "u_product": "Blackberries", "u_date": "1/29/15"}, {"u_serial_number": "15", "u_product": "Blueberries", "u_date": "2/5/15"}, {"u_serial_number": "16", "u_product": "Bananas", "u_date": "2/7/15"}, {"u_serial_number": "17", "u_product": "Strawberries", "u_date": "2/7/15"}]
# Status: 201 Headers: {'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Content-Type': 'application/json'}
编辑:我正在将数据发送到ServiceNow实例。这里是一个描述Python脚本模板的wiki文章。
http://wiki.servicenow.com/index.php?title=Table_API_Python_Examples#gsc.tab=0
这是我用作模板的基本代码块。请注意,根据示例,这适用于单行数据,但在导入多行数据时不适用。
#Need to install requests package for python
#sudo easy_install requests
import requests
# Set the request parameters
url = 'https://myinstance.service-now.com/api/now/table/incident'
user = 'xxxxxxx'
pwd = 'xxxxxxx'
# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}
# Do the HTTP request
response = requests.post(url, auth=(user, pwd), headers=headers ,data='{"short_description":"Test"}')
# Check for HTTP codes other than 200
if response.status_code != 201:
print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
exit()
最佳答案
我已经确认servicenow rest api只能允许每个请求一条记录。
http://wiki.servicenow.com/index.php?title=Table_API#POST_.2Fapi.2Fnow.2Fv1.2Ftable.2F.28tableName.29
我已经切换到JSONV2API来插入多个记录。
http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#insert
多亏丹尼尔帮我指出了正确的故障排除方向。