3

我编写了一个脚本来计算尺寸的大型 csv 文件:27000 行 x 22 列。我如何读取 CSV 文件以便在 matplotlib 中使用它,就像这个线程中的那样?

散点图中的轴范围

理解生成散点图的概念。已尝试通过以下方式解析 csv 文件:

data=csv.reader(open('some_file.csv, 'rb'), delimiter='|', quotechar='"')

但没有成功。

4

3 回答 3

9

这是一个快速的解决方案

def getColumn(filename, column):
    results = csv.reader(open(filename), delimiter="\t")
    return [result[column] for result in results]

然后你可以像这样使用它

time = getColumn("filename",0)
volt = getColumn("filaname",1)

plt.figure("Time/Volt")
plt.xlabel("Time(ms)")
plt.ylabel("Volt(mV)")
plt.plot(time,volt)
于 2012-02-27T22:39:30.940 回答
2

作为一般替代方案,您可能对 Wes McKinney 的 pandas python 包感兴趣:http: //pandas.pydata.org/

它确实改变了我使用 python 进行数据分析的生活。它为 python 提供了类似于 R 的 data.frame 的数据结构,但功能更强大。它建立在 numpy 之上。

它会非常轻松地读取 csv 文件,将数据加载到可以轻松切片和操作的 DataFrame(numpy 数组子类)中。

于 2012-02-28T05:29:34.477 回答
1

那是正确的分隔符吗?你读过文档吗? http://docs.python.org/library/csv.html

data是一个类似文件的对象。您必须对其进行迭代才能访问数据。正如马库斯在他的例子中指出的那样,每一行都是一个列表。

于 2012-02-27T22:40:39.763 回答