List object has no attribute Values error

2024/10/9 20:27:17

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 jsonpayload_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 jsonpayload = {'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.

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.

https://en.xdnf.cn/q/69975.html

Related Q&A

How to resize an image in python, while retaining aspect ratio, given a target size?

First off part of me feels like this is a stupid question, sorry about that. Currently the most accurate way Ive found of calculating the optimum scaling factor (best width and height for target pixel …

How to limit number of followed pages per site in Python Scrapy

I am trying to build a spider that could efficiently scrape text information from many websites. Since I am a Python user I was referred to Scrapy. However, in order to avoid scraping huge websites, I …

Why is Pythons sorted() slower than copy, then .sort()

Here is the code I ran:import timeitprint timeit.Timer(a = sorted(x), x = [(2, bla), (4, boo), (3, 4), (1, 2) , (0, 1), (4, 3), (2, 1) , (0, 0)]).timeit(number = 1000) print timeit.Timer(a=x[:];a.sort(…

How to efficiently unroll a matrix by value with numpy?

I have a matrix M with values 0 through N within it. Id like to unroll this matrix to create a new matrix A where each submatrix A[i, :, :] represents whether or not M == i.The solution below uses a lo…

Anaconda Python 3.6 -- pythonw and python supposed to be equivalent?

According to Python 3 documentation, python and pythonw should be equivalent for running GUI scripts as of 3.6With older versions of Python, there is one Mac OS X quirk that you need to be aware of: pr…

Good way of handling NoneType objects when printing in Python

How do I go about printin a NoneType object in Python?# score can be a NonType object logging.info("NEW_SCORE : "+score)Also why is that sometime I see a comma instead of the + above?

problems with easy_install pycrypto

Im trying install pycrypto on osx with easy_install and Im getting the following error:easy_install pycrypto Searching for pycrypto Reading http://pypi.python.org/simple/pycrypto/ Reading http://pycryp…

What is the most efficient way to do a sorted reduce in PySpark?

I am analyzing on-time performance records of US domestic flights from 2015. I need to group by tail number, and store a date sorted list of all the flights for each tail number in a database, to be re…

Interactive figure with OO Matplotlib

Using Matplotlib via the OO API is easy enough for a non-interactive backend:from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvasfrom matplotlib.figure import Figurefig = Figure(…

nose2 vs py.test with isolated processes

We have been using nosetest for running and collecting our unittests (which are all written as python unittests which we like). Things we like about nose:uses standard python unit tests (we like the st…