不是有效的非字符串序列或映射对象

不是有效的非字符串序列或映射对象

本文介绍了urllib.urlencode: TypeError 不是有效的非字符串序列或映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下代码,但它给了我以下错误:

I am trying to run following code but it is giving me below error:

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 函数不接受单个字符串作为输入,它需要一个类似于字典的东西.

The urlencode function does not take a single string as input, it takes a something like a dictionary.

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

来自文档

urllib.urlencode(query[,doseq])

将映射对象或二元素元组序列转换为百分比编码"字符串,适合作为可选数据参数传递给上面的 urlopen().这对于将表单字段字典传递给 POST 请求很有用.结果字符串是一系列由&"分隔的键=值对字符,其中键和值都使用上面的 quote_plus() 引用.当使用双元素元组序列作为查询参数时,每个元组的第一个元素是键,第二个元素是值.

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.

这篇关于urllib.urlencode: TypeError 不是有效的非字符串序列或映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 16:09