我正在尝试获取地震数据,并将其转换为数组,以便我可以使用该数据来显示地图上的地震。我正在写这个剧本:
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 :(得分:1)
大多数建议都是代码中的注释,但有一些通用的注释:
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')
答案 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))