search
HomeBackend DevelopmentPython TutorialHow to read txt data file into matrix in Python3
How to read txt data file into matrix in Python3Apr 27, 2018 pm 03:42 PM
python3document

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. ]]

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
2 3
5 6
5 6
When A_row = 2, execute A[A_row:] = list[0:3] and the matrix A is:

1 47## is the above code:
2 3
5 6
8 9

What

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

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
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
wpsystem是什么文件夹wpsystem是什么文件夹Sep 01, 2022 am 11:22 AM

wpsystem文件夹是windows应用文件夹;创建WpSystem文件夹是为了存储某些特定“Microsoft Store”应用程序的数据,因此建议不要删该文件夹,因为删除之后就无法使用指定的应用。

winreagent是什么文件夹winreagent是什么文件夹Aug 26, 2022 am 11:23 AM

winreagent是在系统更新或升级的过程中创建的文件夹;该文件夹中通常包含临时文件,当更新或升级失败时,系统将通过还原先前创建的临时文件来回滚到执行更新或升级过程之前的版本。

baidunetdiskdownload是什么文件夹baidunetdiskdownload是什么文件夹Aug 30, 2022 am 10:45 AM

baidunetdiskdownload是百度网盘默认下载文件的文件夹;百度网盘是百度推出的一项云存储服务,只要下载东西到百度网盘里,都会默认保存到这个文件夹中,并且可跨终端随时随地查看和分享。

usmt.ppkg是什么文件usmt.ppkg是什么文件Sep 09, 2022 pm 02:14 PM

“usmt.ppkg”是windows自带的系统还原功能的系统备份文件;Windows系统还原是在不需要重新安装操作系统,也不会破坏数据文件的前提下使系统回到原有的工作状态,PBR恢复功能的备份文件就是“usmt.ppkg”。

mobileemumaster是什么文件mobileemumaster是什么文件Oct 26, 2022 am 11:28 AM

mobileEmuMaster是手机模拟大师的安装文件夹。手机模拟大师是PC电脑模拟运行安卓系统的免费模拟器程序,一款可以让用户在电脑上运行手机应用的软件,支持安装安卓系统中常见的apk执行文件,支持QQ、微信等生活常用应用,达到全面兼容的效果。

备份文件的扩展名通常是什么备份文件的扩展名通常是什么Sep 01, 2022 pm 03:55 PM

备份文件的扩展名通常是“.bak”;bak文件是一个备份文件,这类文件一般在'.bak前面加上应该有原来的扩展名,有的则是由原文件的后缀名和bak混合而成,在生成了某种类型的文件后,就会自动生成它的备份文件。

kml是什么文件的格式kml是什么文件的格式Sep 14, 2022 am 10:39 AM

kml是谷歌公司创建的一种地标性文件格式;该文件用于记录某一地点或连续地点的时间、经度、纬度、海拔等地理信息数据,可以被“Google Earth”和“Google Maps”识别并显示。

config是什么文件夹可以删除吗config是什么文件夹可以删除吗Sep 13, 2022 pm 03:48 PM

config是软件或者系统中的配置文件,不可以删除;该文件是在用户开机时对计算机进行初始化设置,也就是用户对系统的设置都由它来对计算机进行恢复,因此不能删除软件或者系统中的config配置文件,以免造成错误。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)