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.

这是前一个问题的详细描述,但是随着我深入研究python,我对python如何处理csv文件感到更加困惑。

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文件,它必须保持这种方式(例如,不能将它转换为文本文件)。它相当于一个5行由11列阵列或矩阵,或向量。

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.

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

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:

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

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

And so forth...

等等……

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

我正在寻找处理这种数据结构的技术。我对python非常陌生。

3 个解决方案

#1


64

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.

Python的csv模块以行方式处理数据,这是查看此类数据的通常方式。你似乎想要一个专栏式的方法。这是一种方法。

Assuming your file is named myclone.csv and contains

假设您的文件名为myclone。csv和包含

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)

up front, and do this

在前面,做这个。

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

for each row instead of the similar two lines above.

对于每一行,而不是上面相似的两行。

Note: In Python 3.5+, use next(reader, None) or reader.__next__() instead of reader.next()

注意:在Python 3.5+中,使用next(reader, None)或reader.__next__()而不是reader.next()

更多相关文章

  1. 套接字。接受错误24:对许多打开的文件
  2. Python之错误异常和文件处理
  3. python解析json文件读取Android permission说明
  4. 轮询Web服务的最佳方式(例如,对于Twitter应用程序)
  5. python 之 logger日志 字典配置文件
  6. [置顶] Python + C/C++ 嵌入式编程(1):多维数组Numpy.Array(
  7. python通过get方式,post方式发送http请求和接收http响应-urllib
  8. 使用python api递归计算每个Dropbox文件夹大小
  9. 如何在python中播放wav文件?

随机推荐

  1. Activity对象的onCreate方法真是Android
  2. Android的多媒体框架OpenCore介绍
  3. FMOD在Android玩音响系统的抖动问题
  4. 谈Android四大组件之Activity篇
  5. Android 系统(194)---Android实践 -- 设置
  6. Android 列表选择框 Spinner详解及实例
  7. android camera研究
  8. android UI设计,android ui开发,android
  9. Android中集成QQ登陆和QQ空间分享
  10. Android(安卓)权限中文说明