我想从CSV文件中读取数据,并将其解析为字典格式的Python POST请求,当向API发送请求时,该请求将创建内容类型:multipart/form-data auto
。
我尝试过
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
data_android = {
'mailing_details': row["shipping"],
'caroupay':'false',
'price':row['price'],
'description':row['desc'],
'title':row['title'],
'meetup':'false',
'condition':'2',
'mailing':'true',
'collection_id':row['cat']
}
print(row)
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
#print data_android
response = requests.request("POST", url_and,data=data_android,headers=headers)
print(response.text.encode("utf-8"))
但是,请求使用content-type:application/x-www-form-urlencoded
向API发送数据,但API不接受该数据:
'POST /api/3.0/listings/ HTTP/1.1\r
Host: api.testdomain.com\r
Connection: keep-alive\r
Accept-Encoding: gzip, deflate\r
Accept: */*\r
User-Agent: python-requests/2.21.0\r
platform: android\r
Authorization: Token afs7dfa76sd8f7a68sd76f8as6fd8s\r
Content-Length: 179\r
Content-Type: application/x-www-form-urlencoded\r
\r
abcpay=FALSE&description=test1&mailing_details=FREE&price=239.00&title=Pet+Cat+Dog+Carrier+Portable+Breathable+Comfortable&meetup=FALSE&collection_id=45&mailing=TRUE&condition=2'
之后,我尝试使用CSV模块中的默认dictreader选项:
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
#print data_android
response = requests.request("POST", url_and,data=row,headers=headers)
print(response.text.encode("utf-8"))
,它也提供相同的输出。现在,有什么方法可以将CSV数据解析为字典文件中的POST请求?
这是我的完整代码:
import requests
import logging
import os
import json
import csv
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
url_and = "https://api.test.com/api/3.0/listings/"
android_token = '8fas8d7f9a8s7d9f87as9d8f7sa9df7s9f'
headers = {
'Authorization': "Token " + android_token,
'platform': 'android',
}
data_android = {}
with open("listingData1.csv", "r") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
data_android = {
'mailing_details':row['shipping'],
'abcoupay':'false',
'price':row['price'],
'description':row['desc'],
'title':row['title'],
'meetup':'false',
'condition':'2',
'mailing':'true',
'collection_id':row['cat']
}
search_path = row['image']
urls = []
for file in os.listdir(os.getcwd()+search_path):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+search_path+"\\"+file
urls.append(x)
files = {'photo_%s' % x: ('photo_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}
response = requests.request("POST", url_and,data=data_android,headers=headers)
print(response.text.encode("utf-8"))