为了方便使用和记忆,有时候我们会把 numpy.loadtxt() 缩写成np.loadtxt() ,本篇文章主要讲解用它来读取txt文件。读取txt文件我们通常使用 numpy 中的 loadtxt()函数
numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
注:loadtxt的功能是读入数据文件,这里的数据文件要求每一行数据的格式相同。
也就是说对于下面这样的数据是不符合条件的:
123
1 2 4 3 5
接下来举例讲解函数的功能:
1、简单的读取
test.txt
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7<br></span></p>
|
1 2 3 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >import numpy as np
a = np.loadtxt( 'test.txt' ) #最普通的loadtxt
print(a)<br></span></p>
|
输出:
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >[[1. 2. 3. 4.]
[2. 3. 4. 5.]
[3. 4. 5. 6.]
[4. 5. 6. 7.]]<br></span></p>
|
数组中的数都为浮点数,原因为Python默认的数字的数据类型为双精度浮点数
2、skiprows=n:指跳过前n行
test.txt
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >A B C D
2 3 4 5
3 4 5 6
4 5 6 7<br></span></p>
|
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a = np.loadtxt( 'test.txt' , skiprows=1, dtype=int)
print(a)<br></span></p>
|
输出:
1 2 3 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >[[2 3 4 5]
[3 4 5 6]
[4 5 6 7]]<br></span></p>
|
3、comment=‘#’:如果行的开头为#就会跳过该行
test.txt
1 2 3 4 5 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >A B C D
2 3 4 5
3 4 5 6
#A B C D
4 5 6 7<br></span></p>
|
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a = np.loadtxt( 'test.txt' , skiprows=1, dtype=int, comments= '#' )
print(a)<br></span></p>
|
输出:
1 2 3 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >[[2 3 4 5]
[3 4 5 6]
[4 5 6 7]]<br></span></p>
|
4、usecols=[0,2]:是指只使用0,2两列,参数类型为list
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >a = np.loadtxt( 'test.txt' , skiprows=1, dtype=int, comments= '#' ,usecols=(0, 2), unpack=True)
print(a)<br></span></p>
|
输出:
1 2 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >[[2 3 4]
[4 5 6]]<br></span></p>
|
unpack是指会把每一列当成一个向量输出, 而不是合并在一起。 如果unpack为false或者参数的话输出结果如下:
1 2 3 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >[[2 4]
[3 5]
[4 6]]<br></span></p>
|
test.txt
1 2 3 4 5 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >A, B, C, D
2, 3, 4, 5
3, 4, 5, 6
#A B C D
4, 5, 6, 7<br></span></p>
|
5、delimiter:数据之间的分隔符。如使用逗号","。
6、converters:对数据进行预处理
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >def add_one(x):
return int(x)+1 #注意到这里使用的字符的数据结构
a = np.loadtxt( 'test.txt' , dtype=int, skiprows=1, converters={0:add_one}, comments= '#' , delimiter= ',' , usecols=(0, 2), unpack=True)
print a<br></span></p>
|
1 2 3 4 | <p style= "line-height: 1.75em;" ><span style= "font-family: 微软雅黑, "Microsoft YaHei";" >def add_one(x):
return int(x)+1 #注意到这里使用的字符的数据结构
a = np.loadtxt( 'test.txt' , dtype=int, skiprows=1, converters={0:add_one}, comments= '#' , delimiter= ',' , usecols=(0, 2), unpack=True)
print a<br></span></p>
|
以上就是numpy.loadtxt() 读取txt文件的几种方法。更多Python学习推荐:PyThon学习网教学中心。
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)