如何从csv文件读取数据并将python POST请求解析为dict格式

如何从csv文件读取数据并将python POST请求解析为dict格式,python,python-requests,Python,Python Requests,我想从CSV文件中读取数据,并以字典格式将其解析为Python POST请求,这将在向API发送请求时创建一个内容类型:multipart/form data auto 我试过了 打开(“listingData1.csv”,“r”)作为f_输入: csv_输入=csv.DictReader(f_输入) 对于csv_输入中的行: 数据_={ “邮寄详细信息”:第[“邮寄”]行, “caroupay”:“false”, “价格”:行[“价格”], “说明”:行['desc'], “标题”:行[“标题

我想从CSV文件中读取数据,并以字典格式将其解析为Python POST请求,这将在向API发送请求时创建一个内容类型:
multipart/form data auto

我试过了

打开(“listingData1.csv”,“r”)作为f_输入:
csv_输入=csv.DictReader(f_输入)
对于csv_输入中的行:
数据_={
“邮寄详细信息”:第[“邮寄”]行,
“caroupay”:“false”,
“价格”:行[“价格”],
“说明”:行['desc'],
“标题”:行[“标题”],
“meetup”:“false”,
“条件”:“2”,
'邮寄':'真实',
“集合id”:行['cat']
}
打印(行)
搜索路径=行['image']
URL=[]
对于os.listdir(os.getcwd()+搜索路径)中的文件:
如果文件.endswith((“.jpg”、“.jpeg”、“.png”、“.jpg”、“.jpeg”、“.png”):
x=os.getcwd()+“\\”+搜索路径+“\\”+文件
url.append(x)
files={'photo_u%s'%x:('photo_%s.jpg'%x,打开(文件'rb'),'image/jpeg')用于x,文件位于枚举(URL)}
#使用android打印数据
response=requests.request(“POST”,url\u和,data=data\u,headers=headers)
打印(响应.文本.编码(“utf-8”))
但是,请求使用API不接受的
内容类型:application/x-www-form-urlencoded
向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"))
可能的重复可能的重复