gpt4 book ai didi

python 请求发送数组中的文件显示 ValueError : too many values to unpack

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:33 24 4
gpt4 key购买 nike

这里有什么问题吗?

我收到错误显示

ValueError:太多值无法解压

此代码过程是获取文件夹中的所有可用图像,然后将该图像位置放入数组中。然后创建另一个名为files的数组并添加特定格式的图像位置并向API发送POST请求

import requests
import logging
import os
import json

try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

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 = '68as76df87s86df7asd76f87as6df78sfd'
headers = {
'Authorization': "Token " + android_token,
'platform': 'android'
}

data_android = {
'mailing_details':'3',
'abcoupay':'false',
'price':'55.00',
'description':'Test description',
'title':'TEST drill machine and others.',
'meetup':'false',
'condition':'2',
'mailing':'true',
'collection_id':'24'
}

urls = []
for file in os.listdir(os.getcwd()+"/product_images"):
if file.endswith((".jpg",".jpeg",".png",".JPG",".JPEG",".PNG")):
x = os.getcwd()+"\\"+file
urls.append(x)

files = []
x = 0
for file in urls:
files.append("'photo_"+str(x)+"': ('image_"+str(x)+".jpg', open('"+file+"', 'rb'), 'image/jpeg')")
x+=1

# files = {
# 'photo_0': ('image_0.jpg', open('E:/products files/Drill machine/1.jpg', 'rb'), 'image/jpeg'),
# 'photo_1': ('image_1.jpg', open('E:/products files/Drill machine/2.jpg','rb'), 'image/jpeg')
# }

response = requests.request("POST", url_and,data=data_android,files=files,headers=headers)
print(response.text.encode("utf-8"))

错误

Traceback (most recent call last):
File "cookies.py", line 102, in <module>
response = requests.request("POST", url_and,data=data_android,files=files,headers=headers)
File "C:\python27\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\python27\lib\site-packages\requests\sessions.py", line 519, in request
prep = self.prepare_request(req)
File "C:\python27\lib\site-packages\requests\sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\python27\lib\site-packages\requests\models.py", line 316, in prepare
self.prepare_body(data, files, json)
File "C:\python27\lib\site-packages\requests\models.py", line 504, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "C:\python27\lib\site-packages\requests\models.py", line 141, in _encode_files
for (k, v) in files:
ValueError: too many values to unpack

最佳答案

requests.request的files参数应该是一个字典,其中键是文件名,值是文件内容或文件对象。因此,您应该将 files 变量构建为字典:

files = {'image_%s.jpg' % x: open(file, 'rb') for x, file in enumerate(urls)}

如果您还需要内容类型,您可以将字典值设置为包含文件名、文件对象和内容类型的三元组:

files = {'image_%s.jpg' % x: ('image_%s.jpg' % x, open(file, 'rb'), 'image/jpeg') for x, file in enumerate(urls)}

关于python 请求发送数组中的文件显示 ValueError : too many values to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com