2017-02-22 86 views
0

我試圖獲取地震數據,並將其轉換爲數組,以便我可以使用該數據在地圖上可視化地震。我在寫這個劇本:從URL獲取CSV文件並將其轉換爲數組 - Python 2.7

import requests 
import csv 


def csv_to_array(a): 
    b = requests.get(a) 
    my_file = open(b, "rb") 
    for line in my_file: 
     el = [i.strip() for i in line.split(',')] 
     return el 

我導入到另一個模塊,並:

import csvToArray 
data = csvToArray.csv_to_array(
"http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.csv") 
i = 1 
while i < len(data): 
    stuff = data[i].split(',') 
    print stuff[1], stuff[2] 
    lat = float(stuff[1]) 
    lon = float(stuff[2]) 
    x = webMercX(lon, zoom) - cx 
    y = webMercY(lat, zoom) - cy 
    i += 1 

上述腳本的其他功能是不必要的,但是當我運行它,我得到以下錯誤。

while i < len(data): 
TypeError: object of type 'NoneType' has no len() 
+0

因爲'print'不返回任何東西:) ,你沒有從'csv_to_array'返回任何東西' – ZdaR

+0

哇,這是一個愚蠢的錯誤(我現在剛剛解決),但現在我得到以下錯誤: my_file =打開(B,「RB」) 類型錯誤:強迫爲Unicode:需要字符串或緩衝區,響應發現 它無法識別URL作爲字符串 – Oxide

+0

現在你只需返回*第一線*文件。該函數立即結束在第一個'返回' –

回答

1

大多數建議是代碼中的註釋的每一行更換你的第一個功能,但也有少數普通的人:

  1. 更好的名字
  2. 返回立即退出出來的功能,如果你使用yield可以根行中心提供全方位在線

新代碼與學習經驗後:

def csv_to_array(url): # use descriptive variable names 
    response = requests.get(url) 
    lines = response.text.splitlines() # you don't need an open...the data is already loaded 
    for line in lines[1:]: # skip first line (has headers) 
     el = [i.strip() for i in line.split(',')] 
     yield el # don't return, that immediately ends the function 

data = csv_to_array("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.csv") 

for row in data: # don't use indexes, just iterate over the data 
    # you already split on commas. 
    print(row[1], row[2]) # again, better names 
    lat = float(row[1]) 
    lon = float(row[2]) 
#  x = webMercX(lon, zoom) - cx 
#  y = webMercY(lat, zoom) - cy 

代碼懶惰:

import pandas as pd 
pd.read_csv('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.csv') 

enter image description here

+0

對於在谷歌地圖上繪圖,我會看看gmplot。 – gregory

0

您可以用發電機,超過的響應數據進行迭代,並且產生陣列的文件

def csv_to_array(a): 
    response = requests.get(a) 
    # you can access response's body via text attribute 
    for line in response.text.split('\n'): 
     yield [i.strip() for i in line.split(',')] 


list(csv_to_array(some_url)) 
相關問題