📜  如何使用 numpy 在Python读取数字数据或文件?

📅  最后修改于: 2022-05-13 01:55:44.747000             🧑  作者: Mango

如何使用 numpy 在Python读取数字数据或文件?

先决条件Numpy

NumPy 是一个通用的数组处理包。它提供了一个高性能的多维数组对象和用于处理这些数组的工具。本文描述了如何使用 Numpy 从文件中读取数字数据。

数字数据可以以不同格式的文件存在:

  • 数据可以保存在 txt 文件中,其中每行都有一个新的数据点。
  • 数据可以存储在 CSV(逗号分隔值)文件中。
  • 数据也可以存储在 TSV(制表符分隔值)文件中。

有多种在文件中存储数据的方式,以上是一些最常用的存储数值数据的格式。为了实现我们所需的函数,将使用 numpy 的 loadtxt()函数。

方法

  • 导入模块
  • 加载文件
  • 读取数值数据
  • 检索到的打印数据。

下面给出了各种文件格式的一些实现:



所用数据文件的下载链接:

  • 链接1 :gfg_example1.txt
  • 链接2 :gfg_example2.csv
  • 链接3 :gfg_example3.tsv
  • 链接4 :gfg_example4.csv

示例 1:从文本文件中读取数值数据

Python3
# 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}')


Python3
# 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}')


Python3
# 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}')


Python3
# 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}')


输出 :

示例 1 的输出

示例 2:从 CSV 文件中读取数值数据。

蟒蛇3

# 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}')

输出 :

示例 2 的输出

示例 3:从 tsv 文件中读取



蟒蛇3

# 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}')

输出 :

示例 3 的输出

示例 4:仅选择特定行并跳过某些行

蟒蛇3

# 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}')

输出 :

示例 4 的输出