我想将数据传输到Excel工作表。问题是,当我运行整个代码时,我收到一个错误但是当我单独运行它没有错误时它会起作用。
这就是我想要的;
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)
但是当我这样做的时候;
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)
它有效。你能告诉我我的错误在哪里吗?谢谢
答案 0 :(得分:3)
在您的第一个代码块中,您有一个名为dict
的列表,它会隐藏内置的dict
类型。因此,当您尝试在
dict.values
方法时
data = map(dict.values, data[u'output'][u'resultset'][u'record'][u'data'])
Python在您的列表中查找名为.values()
的{{1}}方法,而不是使用内置的dict
方法,但找不到这样的方法。
因此,请将该列表的名称更改为不会与内置名称冲突的内容。