Home Backend Development Python Tutorial How to read txt data file into matrix in Python3

How to read txt data file into matrix in Python3

Apr 27, 2018 pm 03:42 PM
python3 document

Below I will share with you a method of reading txt data files into a matrix in Python3. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

1. Example program:

'''
数据文件:2.txt内容:(以空格分开每个数据)
1 2 2.5
3 4 4
7 8 7
'''

from numpy import *
A = zeros((3,3),dtype=float)  #先创建一个 3x3的全零方阵A,并且数据的类型设置为float浮点型

f = open('2.txt')        #打开数据文件文件
lines = f.readlines()      #把全部数据文件读到一个列表lines中
A_row = 0            #表示矩阵的行,从0行开始
for line in lines:       #把lines中的数据逐行读取出来
  list = line.strip('\n').split(' ')   #处理逐行数据:strip表示把头尾的'\n'去掉,split表示以空格来分割行数据,然后把处理后的行数据返回到list列表中
  A[A_row:] = list[0:3]          #把处理后的数据放到方阵A中。list[0:3]表示列表的0,1,2列数据放到矩阵A中的A_row行
  A_row+=1                #然后方阵A的下一行接着读
  #print(line)

print(A)  #打印 方阵A里的数据

打印结果:
[[ 1.  2.  2.5]
 [ 3.  4.  4. ]
 [ 7.  8.  7. ]]
Copy after login

2. The logic of reading data into the matrix:

is a simple explanation. For example, we need to:

1 2 3

4 5 6

7 8 9

Read into the matrix, take the above code as an example:

When When A_row =0, after executing A[A_row:] = list[0:3], the matrix A is:

##123123123

When A_row = 1, execute A[A_row:] = list[0:3] after matrix A Is:

##144
23
56
56
When A_row = 2, execute A[A_row:] = list[0:3] and the matrix A is:

1 47## is the above code:
23
56
89

What

for line in lines:           #先把逐行数据取出来 
  list = line.strip('\n').split(' ')   #再通过处理,放回到list列表中 
  A[A_row:] = list[0:3]          #然后把list列表的数据放到矩阵中 
  A_row+=1
Copy after login

does.

Related recommendations:


How to read TXT files in Python

How to generate a list by reading file names in python

The above is the detailed content of How to read txt data file into matrix in Python3. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to recover expired WeChat files? Can expired WeChat files be recovered? How to recover expired WeChat files? Can expired WeChat files be recovered? Feb 22, 2024 pm 02:46 PM

How to recover expired WeChat files? Can expired WeChat files be recovered?

Photos cannot open this file because the format is not supported or the file is corrupted Photos cannot open this file because the format is not supported or the file is corrupted Feb 22, 2024 am 09:49 AM

Photos cannot open this file because the format is not supported or the file is corrupted

Preparing for removal takes a long time in Windows 11/10 Preparing for removal takes a long time in Windows 11/10 Feb 19, 2024 pm 07:42 PM

Preparing for removal takes a long time in Windows 11/10

Can Tmp format files be deleted? Can Tmp format files be deleted? Feb 24, 2024 pm 04:33 PM

Can Tmp format files be deleted?

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? How to transfer files from Quark Cloud Disk to Baidu Cloud Disk? Mar 14, 2024 pm 02:07 PM

How to transfer files from Quark Cloud Disk to Baidu Cloud Disk?

Upload failed, file cannot be empty, inconsistent error Upload failed, file cannot be empty, inconsistent error Feb 19, 2024 pm 01:21 PM

Upload failed, file cannot be empty, inconsistent error

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code.

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

What is hiberfil.sys file? Can hiberfil.sys be deleted?

See all articles