我正在尝试创建一个脚本,该脚本遍历CSV的每一行并发布到API调用,但得到的是TypeError:需要类似字节的对象,而不是"dict"。
CSV只有3列。通常情况下,我可以传递一个对象行[2]等,但我不明白为什么会在这里发生,也不明白如何修复它?
谢谢
import csv
import requests
from time import sleep
with open('/Users/me/Downloads/mailgun_test.csv',newline='') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
files = {
'from': ('Sender <sender@sender.com>'),
'to': str(row[0]),
'subject': ('Your '+str(row[2])+' Verificiation Has Expired'),
'template': ('tpp_expired'),
'h:X-Mailgun-Variables': ({"company_name": str(row[1]), "verification_file_type": str(row[2])})
}
r = requests.post('https://api.mailgun.net/v3/domain/messages', files=files, auth=('api', 'key................'))
print("=========================")
print(r)
print(r.text)
sleep(2)```
发送post调用正文中的数据。您可以参考文档中的示例:https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-通过api
注意:切换到文档中的python(必需(语言选项卡。
以上文档示例:
def send_simple_message():
payload = {"from": "Excited User <mailgun@YOUR_DOMAIN_NAME>",
"to": ["bar@example.com", "YOU@YOUR_DOMAIN_NAME"],
"subject": "Hello",
"text": "Testing some Mailgun awesomness!"}
return requests.post( "https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
data=payload)