'List' Object Has No Attribute 'Values' Error

'List' Object Has No Attribute 'Values' Error

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.

Here is what I want;

from xlwings import Workbook, Sheet, Range, Chart
import requests
import json

payload_city = {'cityId':3969, 'cmd':'districts'}
url = ""
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 = ""
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 my mistake is? Thank you.

1 Answer

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

data = map(dict.values, data[u'output'][u'resultset'][u'record'][u'data'])

Python looks for a .values() method in your list that's named dict instead of using the built-in dict.values() method, and it can't find such a method.

So change the name of that list to something that won't clash with a built-in name.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.