gpt4 book ai didi

python - urllib.urlencode : TypeError not a valid non-string sequence or mapping object

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:32 43 4
gpt4 key购买 nike

我正在尝试运行以下代码,但出现以下错误:

Traceback (most recent call last):  File "put_message.py", line 43, in <module>translatedWord=getTranslatedValue(source_lang,source_word,dest_lang,apiKey)  File "put_message.py", line 22, in getTranslatedValue
source_word=urllib.urlencode(source_word)
File "/usr/lib/python2.7/urllib.py", line 1318, in urlencode
raise TypeError
TypeError: not a valid non-string sequence or mapping object

我的程序如下:

将数据从一种语言翻译成另一种语言的脚本

import MySQLdb
import json
import urllib, urllib2
import requests
from pprint import pprint
import sys


def getTranslatedValue(source_lang,source_word,dest_lang,apiKey):


source_word=urllib.urlencode(source_word)
url='https://www.googleapis.com/language/translate/v2?key=%s&q=%s&source=%s&target=%s',(apiKey,source_word,source_lang,dest_lang)
j = urllib2.urlopen(url)
j_obj = json.load(j)
j.close()
translatedText=j_obj['data']['translations'][0]['translatedText']
return translatedText


# Open database connection
db = MySQLdb.connect(host,user,password)

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
getCategory = " SELECT entity_id,attribute_id,VALUE FROM magento19_org.catalog_category_entity_text WHERE attribute_id IN(44,47,48) UNION ALL SELECT entity_id,attribute_id,VALUE FROM magento19_org.catalog_category_entity_varchar WHERE attribute_id IN(41,46)"
cursor.execute(getCategory)
rows = cursor.fetchall()
for row in rows:
source_word=row[2]
translatedWord=getTranslatedValue(source_lang,source_word,dest_lang,apiKey)
entity_id=row[0]
attribute_id=row[1]
value=row[2]
insertCategoryTranslate="insert into googletranslate.category_translate(entity_id ,attribute_id ,value,french_translate )values(%s,%s,%s,%s)"
cursor.execute(insertCategoryTranslate,(str(entity_id),str(attribute_id),str(value),str(translatedWord)))
db.commit()
# disconnect from server
db.close()

最佳答案

urlencode 函数不接受单个字符串作为输入,它接受类似字典的东西。

data = urlencode({'key': apiKey, 'q': source_word, ...)
urllib2.urlopen("http://....", data)

来自Documentation ,

urllib.urlencode(query[, doseq])

Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string, suitable to pass to urlopen() above as the optional data argument. This is useful to pass a dictionary of form fields to a POST request. The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using quote_plus() above. When a sequence of two-element tuples is used as the query argument, the first element of each tuple is a key and the second is a value.

关于python - urllib.urlencode : TypeError not a valid non-string sequence or mapping object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947568/

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