2014-04-08 59 views
0

我正在嘗試使用google api創建美國的強度圖。我從公開可用的數據集中獲取python中的數據,然後將其傳遞給UI端。下面是它未捕獲的錯誤:行0的行類型無效

query_string = 'SELECT state, count(*) FROM [{0}] GROUP by state;'.format(_DATABASE_NAME) 
births = self.run_query(query_string, filename='data/states.json') 
rows = births[u'rows'] 
states = [] 
for row in rows: 
    name = row[u'f'][0][u'v'] 
    num = row[u'f'][1][u'v'] 
    if name == None: name = u'None' 
     state = {'state':unicode.encode(name), 'total':int(num)} 
     states = states + [state] 
states.insert(0, ('State','No of users')) 
logging.info(json.encode(states)) 
context = {"states": json.encode(states)} 
self.render_response('index.html', context) 

這是怎麼json.encode(州)看起來像代碼 -

[["State","No of users"],{"state":"AL","total":1957392},{"state":"AK","total":324183},{"state":"AZ","total":1837630},{"state":"AR","total":1062588},{"state":"CA","total":13794114},........... 

,這裏是index.html文件 -

<html> 
<head> 
    <script type='text/javascript' src='https://www.google.com/jsapi'></script> 
    <script type='text/javascript'> 
    google.load('visualization', '1', {'packages': ['geomap']}); 
    google.setOnLoadCallback(drawMap); 

    function drawMap() { 
     console.log("In draw map function"); 
     var test = {{states|safe}}; 
     console.log(test); console.log(test); 
     var data = google.visualization.arrayToDataTable({{states|safe}}); 

     var options = {}; 
     options['region'] = 'US' 
     options['dataMode'] = 'regions'; 

     var container = document.getElementById('map_canvas'); 
     var geomap = new google.visualization.GeoMap(container); 
     geomap.draw(data, options); 
    }; 
    </script> 
</head> 

<body> 
    <div id='map_canvas'></div> 

</body> 

</html> 

爲什麼我得到這種錯誤?我看到在瀏覽器控制檯以下 - 未捕獲的錯誤:無效行類型的行0 LDA TP drawMap(指數:12)

+0

出於好奇,什麼是'json.encode'?通常,會使用'json.dumps'或類似的東西... – mgilson

回答

0

一般來說,google.visualization.arrayToDataTable預計陣列在python的陣列(列表的列表)。你在列表中有一堆dict,我不認爲應該在那裏。

你或許應該有:

state = [unicode.encode(name), int(num)] 
states.append(state) # this is better than states = states + [state] 
相關問題