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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With