如何用numpy在Python中读取数字数据或文件
NumPy是一个通用的数组处理包。它提供了一个高性能的多维数组对象和处理这些数组的工具。本文描述了如何使用Numpy从文件中读取数字数据。
数字数据可以存在于不同格式的文件中。
- 数据可以保存在一个txt文件中,每一行都有一个新的数据点。
- 数据可以存储在CSV(逗号分隔值)文件中。
- 数据也可以存储在TSV(tab separated values)文件中。
在文件中存储数据有多种方式,以上是一些最常用的存储数字数据的格式。为了实现我们需要的功能,将使用numpy的loadtxt()函数。
语法:
numpy.loadtxt(fname, dtype='float', comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
参数:
fname : 要读取的文件、文件名或生成器。如果文件名的扩展名是.gz或.bz2,则首先对文件进行解压缩。注意,对于Python 3k,生成器应该返回字节字符串。
dtype :结果数组的数据类型;默认:浮点。如果这是一个结构化的数据类型,产生的数组将是一维的,每一行将被解释为数组的一个元素。
delimiter : 用来分隔数值的字符串。默认情况下,它是任何空白。_
converters : 一个字典,将列号映射到一个将该列转换为浮点数的函数。例如,如果第0列是一个日期字符串:converters = {0: datestr2num}。默认值。无。
skiprows :跳过第一个skiprows行;默认:0。
返回: ndarray
步骤
- Import module
- Load file
- 读取数字数据
- 打印检索到的数据。
下面是对各种文件格式的一些实现。
例子1:从文本文件中读取数字数据
# Importing libraries that will be used
import numpy as np
# Setting name of the file that the data is to be extracted from in python
filename = 'gfg_example1.txt'
# Loading file data into numpy array and storing it in variable called data_collected
data_collected = np.loadtxt(filename)
# Printing data stored
print(data_collected)
# Type of data
print(
f'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}')
输出 :
例子2:从CSV文件中读取数字数据。
# Importing libraries that will be used
import numpy as np
# Setting name of the file that the data is to be extracted from in python
# This is a comma separated values file
filename = 'gfg_example2.csv'
# Loading file data into numpy array and storing it in variable.
# We use a delimiter that basically tells the code that at every ',' we encounter,
# we need to treat it as a new data point.
# The data type of the variables is set to be int using dtype parameter.
data_collected = np.loadtxt(filename, delimiter=',', dtype=int)
# Printing data stored
print(data_collected)
# Type of data
print(
f'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}')
输出 :
例子3:从tsv文件中读取信息
# Importing libraries that will be used
import numpy as np
# Setting name of the file that the data is to be extracted from in python
# This
filename = 'gfg_example3.tsv'
# Loading file data into numpy array and storing it in variable called data_collected
# We use a delimiter that basically tells the code that at every ',' we encounter,
# we need to treat it as a new data point.
data_collected = np.loadtxt(filename, delimiter='\t')
# Printing data stored
print(data_collected)
# Type of data
print(
f'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}')
输出 :
例子4:只选择特定的行并跳过一些行
# Importing libraries that will be used
import numpy as np
# Setting name of the file that the data is to be extracted from in python
filename = 'gfg_example4.csv'
# Loading file data into numpy array and storing it in variable called data_collected
data_collected = np.loadtxt(
filename, skiprows=1, usecols=[0, 1], delimiter=',')
# Printing data stored
print(data_collected)
# Type of data
print(
f'Stored in : {type(data_collected)} and data type is : {data_collected.dtype}')
输出 :