从URL获取CSV文件并将其转换为数组 - Python 2.7

时间:2017-02-22 17:09:59

标签: python arrays csv

我正在尝试获取地震数据,并将其转换为数组,以便我可以使用该数据来显示地图上的地震。我正在写这个剧本:

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()

2 个答案:

答案 0 :(得分:1)

大多数建议都是代码中的注释,但有一些通用的注释:

  1. 使用更好的名字
  2. 如果您使用yield,您可以立即退出该功能
  3. 具有学习经验的新代码:

    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

答案 1 :(得分: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))