我正在尝试(第一次)使用带有Python请求模块的API .
我需要从API获取一些数据并将其解析为JSON . 在测试查询时,我能够成功获得Postman for chrome的预期响应 .
但是,当我尝试使用Python执行相同的代码时,数据编码不正确 . 我在我的数据上尝试过.encode('utf-8) .encode(' utf-8)没有成功 . 我已经阅读了有关python howto(https://docs.python.org/2/howto/unicode.html)编码的文章而没有运气 .
我的代码:
导入请求r = requests.get(“http://company.vtexcommercestable.com.br/api/oms/pvt/orders?per_page=100”,headers = {“Accept”:“application / json”,“Content型 “:” 应用/ JSON”, “X-VTEX-API-APPTOKEN”: “密码”, “X-VTEX-API-的AppKey”: “testemail@gmail.com”}); data = r.json()print r
结果:
{u'stats':{u'stats':{u'totalItems':{u'Count':113,u'Min':0.0,u'Max':0.0,u'Sum':0.0,u'缺少':0,u'SumOfSquares':0.0,u'StdDev':0.0,u'Facets':{},u'Mean':0.0},u'totalValue':
我需要删除“你”..并添加保留拉丁字符(重音和“ñ”)
非常感谢帮助!
2 回答
我能够在安装unicodecsv软件包并用原始csv替换后解决问题
然后,我能够csv.writerow Unicode字符没有问题 .
这些是unicode字符 - see this answer for a great explanation .
在逻辑中将这些作为常规字符进行评估时不应该有任何问题,因此不必担心“删除它们” .