如何导入一个csv文件使用python的头完好,其中第一列是非数值 [英] How to import a csv file using python with headers intact, where first column is a non-numerical

查看:114
本文介绍了如何导入一个csv文件使用python的头完好,其中第一列是非数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个前一个问题的细节,但是当我深入到python,我只是更多的困惑,如何python处理csv文件。

This is an elaboration of a previous question, but as I delve deeper into python, I just get more confused as to how python handles csv files.

一个csv文件,它必须保持那种方式(例如,不能将其转换为文本文件)。它相当于一个5行乘11列的数组或矩阵或向量。

I have a csv file, and it must stay that way (e.g., cannot convert it to text file). It is the equivalent of a 5 rows by 11 columns array or matrix, or vector.

我一直在尝试使用我在这里找到的各种方法读取csv,其他地方(例如 python.org ),以便它保留列和行之间的关系,其中第一行和第一列=非数值。其余的都是浮点值,并包含正和负浮点的混合。

I have been attempting to read in the csv using various methods I have found here and other places (e.g. python.org) so that it preserves the relationship between columns and rows, where the first row and the first column = non-numerical values. The rest are float values, and contain a mixture of positive and negative floats.

我想做的是导入csv并在python中编译它,要引用列标题,它将返回存储在行中的关联值。例如:

What I wish to do is import the csv and compile it in python so that if I were to reference a column header, it would return its associated values stored in the rows. For example:

>>> workers, constant, age
>>> workers
    w0
    w1
    w2
    w3
    constant
    7.334
    5.235
    3.225
    0
    age
    -1.406
    -4.936
    -1.478
    0

等等...

我正在寻找处理这种数据结构的技术。我对python很新。

I am looking for techniques for handling this kind of data structure. I am very new to python.

推荐答案

Python的csv模块按行处理数据,这是查看数据。你似乎想要一个列方法。

Python's csv module handles data row-wise, which is the usual way of looking at such data. You seem to want a column-wise approach. Here's one way of doing it.

假设您的档案名称为 myclone.csv ,并包含

Assuming your file is named myclone.csv and contains

workers,constant,age
w0,7.334,-1.406
w1,5.235,-4.936
w2,3.2225,-1.478
w3,0,0

此代码应该给你一个想法或两个:

this code should give you an idea or two:

>>> import csv
>>> f = open('myclone.csv', 'rb')
>>> reader = csv.reader(f)
>>> headers = reader.next()
>>> headers
['workers', 'constant', 'age']
>>> column = {}
>>> for h in headers:
...    column[h] = []
...
>>> column
{'workers': [], 'constant': [], 'age': []}
>>> for row in reader:
...   for h, v in zip(headers, row):
...     column[h].append(v)
...
>>> column
{'workers': ['w0', 'w1', 'w2', 'w3'], 'constant': ['7.334', '5.235', '3.2225', '0'], 'age': ['-1.406', '-4.936', '-1.478', '0']}
>>> column['workers']
['w0', 'w1', 'w2', 'w3']
>>> column['constant']
['7.334', '5.235', '3.2225', '0']
>>> column['age']
['-1.406', '-4.936', '-1.478', '0']
>>>

要将数字值转换为浮动广告,请添加此

To get your numeric values into floats, add this

converters = [str.strip] + [float] * (len(headers) - 1)

,并执行此操作

for h, v, conv in zip(headers, row, converters):
  column[h].append(conv(v))

代表每行,而不是上面类似的两行。

for each row instead of the similar two lines above.

这篇关于如何导入一个csv文件使用python的头完好,其中第一列是非数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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