提取csv文件特定列以在Python中列出 [英] Extract csv file specific columns to list in Python

查看:1518
本文介绍了提取csv文件特定列以在Python中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,所以请和我一起露面。我想做的是在地图上绘制特定风暴的纬度和经度值使用matplotlib,底图,python等。我的问题是,我试图提取风暴的纬度,经度和名称地图,但我不断得到错误行41-44之间,我试图提取列到列表中。有人可以帮助我想出这个。先感谢。



以下是文件的样子:

  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

我想让列表看起来像下面这样:

  latitude = [21.6N,22.0N,23.4N] 
longitude = [93.3W,93.5W,93.8W]
name = [Audrey,Audrey]

/ p>

  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 b $ b zipped = zip(* reader)

latitude = zipped [3]
longitude = zipped [4]
names = zipped [1]
x,y = m(经度,纬度)



这里是我收到的最后一个错误信息/ traceback:


跟踪(最近一次调用):

文件/home/darealmzd/lstorms.py,第42行, / p>

header = reader.next()

_csv.Error:在无引号字段中看到的新行字符 - 是否需要打开通用文件 - 新线模式?



解决方案

这看起来像是代码中行尾的问题。如果您要使用所有其他科学包,您也可以使用 Pandas 作为CSV阅读部分,这是更多的比$ csv 模块更强大和更有用:

  import pandas 
colnames = ['year','name','city','latitude','longitude']
data = pandas.read_csv('test.csv',names = colnames)

如果您想在问题中列出,您现在可以:

  names = data.name.tolist()
latitude = data.latitude.tolist()
longitude = data.longitude.tolist()


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

这篇关于提取csv文件特定列以在Python中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆