2017-02-22 88 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)) 
相关问题