python对csv数据提取某列的某些行,提取csv文件特定列以在Python中列出

I'm a newb to Python so please bare with me. What I'm trying to do is plot the latitude and longitude values of specific storms on a map using matplotlib,basemap,python, etc. My problem is that I'm trying to extract the latitude, longitude, and name of the storms on map but I keep getting errors between lines 41-44 where I try to extract the columns into the list. Could someone please help me figure this out. Thanks in advance.

Here is what the file looks like:

1957,AUDREY,HU, 21.6N, 93.3W

1957,AUDREY,HU,22.0N, 93.4W

1957,AUDREY,HU,22.6N, 93.5W

1957,AUDREY,HU,23.2N, 93.6W

I want the list to look like the following:

latitude = [21.6N,22.0N,23.4N]

longitude = [93.3W, 93.5W,93.8W]

name = ["Audrey","Audrey"]

Here's what I have so far:

data = np.loadtxt('louisianastormb.csv',dtype=np.str,delimiter=',',skiprows=1)

'''print data'''

data = np.loadtxt('louisianastormb.csv',dtype=np.str,delimiter=',',skiprows=0)

f= open('louisianastormb.csv', 'rb')

reader = csv.reader(f, delimiter=',')

header = reader.next()

zipped = zip(*reader)

latitude = zipped[3]

longitude = zipped[4]

names = zipped[1]

x, y = m(longitude, latitude)

Here's the last error message/traceback I received:

Traceback (most recent call last):

File "/home/darealmzd/lstorms.py", line 42, in

header = reader.next()

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

解决方案

This looks like a problem with line endings in your code. If you're going to be using all these other scientific packages, you may as well use Pandas for the CSV reading part, which is both more robust and more useful than just the csv module:

import pandas

colnames = ['year', 'name', 'city', 'latitude', 'longitude']

data = pandas.read_csv('test.csv', names=colnames)

If you want your lists as in the question, you can now do:

names = data.name.tolist()

latitude = data.latitude.tolist()

longitude = data.longitude.tolist()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值