在Python中将csv文件导入矩阵/数组 [英] Import csv file into a matrix/Array in Python

查看:2149
本文介绍了在Python中将csv文件导入矩阵/数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图导入包含字符串和数字矩阵的大csv文件到Python中的数组/矩阵。在MATLAB中,我用来加载文件,并简单地将它分配给一个矩阵,但似乎是一个有点棘手的Python。可能有人建议吗?我在Python是相当新的。谢谢大家。

I am trying to import big csv files which contain both string and numeric matrix of data into Arrays/matrices in Python. In MATLAB I used to load the file and simply assign it to a matrix but it seems to be a bit tricky in Python. Could somebody advise please? I am quite new in Python. Thanks all.

推荐答案

您可以使用内置的 csv 模块将数据加载到多维列表中:

You can use the built-in csv module to load your data to a multidimensional list:

import csv

with open('data.csv', 'rb') as f:
    reader = csv.reader(f)
    data_as_list = list(reader)

print data_as_list
# [['data1', 1],
#  ['data2', 2],
#  ['data3', 3]]

这篇关于在Python中将csv文件导入矩阵/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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