【问题标题】:Create 2d array from csv file in python 3在 python 3 中从 csv 文件创建二维数组
【发布时间】:2016-04-18 02:45:22
【问题描述】:

我有一个 .csv 文件,其中包含类似以下示例的数据

A,B,C,D,E,F,
1,-1.978,7.676,7.676,7.676,0,
2,-2.028,6.081,6.081,6.081,1,
3,-1.991,6.142,6.142,6.142,1,
4,-1.990,65.210,65.210,65.210,5,
5,-2.018,8.212,8.212,8.212,5,
6,54.733,32.545,32.545,32.545,6,
..and so on

格式不变。

我想将文件加载到变量“log”中并作为 log[row][column] 访问它们

example
log[0][2] should give C
log[3][1] should give -1

如果使用此代码

file = open('log.csv')
log = list(file)

当我使用此代码时,我只得到一维。日志[行]

他们有什么方法可以直接存储它们吗?

例如

read the file
split with '\n'
split again with ','

谢谢!!

【问题讨论】:

    标签: arrays csv python-3.x


    【解决方案1】:

    试试这个

    log = [line.split(',') for line in open('log.csv')]
    

    【讨论】:

    • 完美运行!!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多