Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'List' object has no attribute 'Values' error

Tags:

python

excel

vba

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 = "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 my mistake is? Thank you.

like image 285
Shalafister Avatar asked Mar 01 '15 11:03

Shalafister


People also ask

How do you fix an object that has no attribute?

The Python "AttributeError: 'list' object has no attribute" occurs when we access an attribute that doesn't exist on a list. To solve the error, access the list element at a specific index or correct the assignment.

How do you fix attribute errors in Python?

To avoid the AttributeError in Python code, a check should be performed before referencing an attribute on an object to ensure that it exists. The Python help() function can be used to find out all attributes and methods related to the object.

What does object has no attribute mean?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.

Why am I getting AttributeError object has no attribute?

If you are getting an object that has no attribute error then the reason behind it is because your indentation is goofed, and you've mixed tabs and spaces. Run the script with python -tt to verify.


1 Answers

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.

like image 145
PM 2Ring Avatar answered Oct 01 '22 07:10

PM 2Ring