reclist="\"MOBEEVHDBBYSQFC8\",\"MOBE9J587QGMXBB7\""
print reclist
data= {
"ids": [
reclist #"MOBEEVHDBBYSQFC8","MOBE9J587QGMXBB7"
]
}
print data
输出:`
"MOBEEVHDBBYSQFC8","MOBE9J587QGMXBB7"`
{'ids': [' "MOBEEVHDBBYSQFC8","MOBE9J587QGMXBB7" ']}
但期望输出是:
"MOBEEVHDBBYSQFC8","MOBE9J587QGMXBB7"
{'ids': ["MOBEEVHDBBYSQFC8","MOBE9J587QGMXBB7"]}
当我打印rectlist
时,它没有显示额外的'',但当我在reclist
中使用data
时,由于reclist
中的额外'',显示错误urllib2.HTTPError: HTTP Error 400: Bad Request
,因为我必须将此数据添加到urllib2.Request
。如何在数据中使用reclist
时删除额外的''
答案 0 :(得分:1)
reclist
是一个不可迭代的字符串,您可以使用ast.literal_eval
直接将其转换为元组:
In [60]: import ast
In [61]: reclist="\"MOBEEVHDBBYSQFC8\",\"MOBE9J587QGMXBB7\""
In [62]: ast.literal_eval(reclist)
Out[62]: ('MOBEEVHDBBYSQFC8', 'MOBE9J587QGMXBB7')
答案 1 :(得分:1)
您还可以split()
和strip()
双引号:
>>> reclist="\"MOBEEVHDBBYSQFC8\",\"MOBE9J587QGMXBB7\""
>>> [item.strip('"') for item in reclist.split(",")]
['MOBEEVHDBBYSQFC8', 'MOBE9J587QGMXBB7']