提取 csv 文件特定的列以在 Python 中列出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19486369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract csv file specific columns to list in Python
提问by mikez1
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.
我想要做的是使用 matplotlib、底图、python 等在地图上绘制特定风暴的纬度和经度值。我的问题是我试图提取风暴的纬度、经度和名称map 但我在第 41-44 行之间不断出现错误,我尝试将列提取到列表中。有人可以帮我解决这个问题。提前致谢。
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, inheader = reader.next()
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
回溯(最近一次通话):
文件“/home/darealmzd/lstorms.py”,第 42 行,在header = reader.next()
_csv.Error: 在未加引号的字段中看到换行符 - 您是否需要以通用换行符模式打开文件?
采纳答案by chthonicdaemon
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 Pandasfor the CSV reading part, which is both more robust and more useful than just the csv
module:
这看起来像是代码中的行尾问题。如果您打算使用所有这些其他科学软件包,您也可以将Pandas用于 CSV 读取部分,它比csv
模块更强大且更有用:
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()
回答by Ben Southgate
A standard-lib version (no pandas)
标准库版本(没有熊猫)
This assumes that the first row of the csv is the headers
这假设 csv 的第一行是标题
import csv
# open the file in universal line ending mode
with open('test.csv', 'rU') as infile:
# read the file as a dictionary for each row ({header : value})
reader = csv.DictReader(infile)
data = {}
for row in reader:
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
# extract the variables you want
names = data['name']
latitude = data['latitude']
longitude = data['longitude']
回答by person the human
import csv
from sys import argv
d = open("mydata.csv", "r")
db = []
for line in csv.reader(d):
db.append(line)
# the rest of your code with 'db' filled with your list of lists as rows and columbs of your csv file.