Python “列表”对象没有属性“值”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28793202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'List' object has no attribute 'Values' error
提问by Shalafister
I would like to get the data to Excel worksheet. The problem is when I run the whole code I receive an error but when I run it separately no error it works.
我想将数据导入 Excel 工作表。问题是当我运行整个代码时,我收到一个错误,但是当我单独运行它时,它没有错误。
Here is what I want;
这是我想要的;
from xlwings import Workbook, Sheet, Range, Chart
import requests
import json
payload_city = {'cityId':3969, 'cmd':'districts'}
url = "https://www.garantimortgage.com/apps/Socket/Webservice.ashx"
r_city = requests.post(url, data=payload_city)
data_city = json.loads(r_city.text) #json to python data structure conversion
wb = Workbook()
dict = data_city[:] #translation in to dictionary
for i in list(range(len(dict))):
print data_city[i]["DistrictName"]
payload_district = {'cityId':data_city[i]["CityId"], 'lbDistricts':data_city[i]["DistrictCode"], 'criter':149,'startdate':'2003-01','cmd':'result','areaCode':data_city[i]["AreaWideCode"]}
r_district = requests.post(url, data=payload_district)
data = json.loads(r_district.text)
data = map(dict.values, data[u'output'][u'resultset'][u'record'][u'data']) #---->NOT OK.
for row in data:
Range("A1").value = zip(*data)
But when I run this as;
但是当我运行它时;
from xlwings import Workbook, Sheet, Range, Chart
import requests
import json
payload = {'cityId':3969, 'lbDistricts':599, 'criter':149,'startdate':'2003-01','cmd':'result','areaCode':18439}
url = "https://www.garantimortgage.com/apps/Socket/Webservice.ashx"
r = requests.post(url, data=payload)
wb = Workbook()
#wb = Workbook.caller()
data = json.loads(r.text)
data = map(dict.values, data[u'output'][u'resultset'][u'record'][u'data'])
for row in data:
Range("A1").value = zip(*data)
it works. Could you please tell me where is my mistake?. Thank you
有用。你能告诉我我的错误在哪里吗?谢谢
采纳答案by PM 2Ring
In your first code block you have a list named dict
, which shadows the built-in dict
type. So when you try to use the dict.values
method in
在您的第一个代码块中,您有一个名为 的列表dict
,它隐藏了内置dict
类型。所以当你尝试dict.values
在
data = map(dict.values, data[u'output'][u'resultset'][u'record'][u'data'])
Python looks for a .values()
method in yourlist that's named dict
instead of using the built-in dict.values()
method, and it can't find such a method.
Python 会.values()
在您的列表中查找已命名dict
的dict.values()
方法,而不是使用内置方法,但它找不到这样的方法。
So change the name of that list to something that won't clash with a built-in name.
因此,将该列表的名称更改为不会与内置名称冲突的名称。