5 python numpy matplotlib scipy python-2.7
我有一个数据文件,我需要阅读.我知道要用Python读取文件,你必须做类似的事情:
file = open(fileLocaion, 'r+')
Run Code Online (Sandbox Code Playgroud)
但我不知道该做什么特别读.我的数据是列.因此x,一列中的y值和另一列中的值,顶部是标题.数据(我的文本文件a.txt)看起来像
Charge (1x), Ch A, Run #1
Time ( s ) Charge (1x) ( µC )
0.0000 0.021
0.1000 0.021
0.2000 0.021
0.3000 0.021
0.4000 0.021
0.5000 0.021
0.6000 0.021
Run Code Online (Sandbox Code Playgroud)
所以第一次的值是0.0000第一次充电值0.021.我希望能够将它带入Python并用于matplotlib绘制它.但我无法弄清楚如何阅读这些数据.
如果您要使用matplotlib绘制它,可能最简单的方法是使用numpy.loadtxt [docs],因为无论如何你都会安装numpy:
>>> import numpy
>>> d = numpy.loadtxt("mdat.txt", skiprows=2)
>>> d
array([[ 0. , 0.021],
[ 0.1 , 0.021],
[ 0.2 , 0.021],
[ 0.3 , 0.021],
[ 0.4 , 0.021],
[ 0.5 , 0.021],
[ 0.6 , 0.021]])
Run Code Online (Sandbox Code Playgroud)
请注意,我必须在skiprows=2此处添加以跳过标题.那么时间d[:,0]和收费d[:,1],或者你可以明确地得到它们loadtxt:
>>> times, charges = numpy.loadtxt("mdat.txt", skiprows=2, unpack=True)
>>> times
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6])
>>> charges
array([ 0.021, 0.021, 0.021, 0.021, 0.021, 0.021, 0.021])
Run Code Online (Sandbox Code Playgroud)