Read file to matrix python

Read text file into matrix python

Your code works perfectly for me. I get that error if I add two new lines at the end of the file which contains the matrix, so the problem must be in the data file.

2 Answers 2

You need to strip where you are using the value.

if line.rstrip(») does not do what you think it does. Read up more on .strip().

with open(filename, 'r') as filename: adjmtrx = [[int(num) for num in line.strip().split(' ')] for line in filename] print(adjmtrx) # [[0, 3, 4, 5, 2], [3, 0, 2, 4, 0], [4, 2, 0, 1, 0], [5, 4, 1, 0, 0], [2, 0, 0, 0, 0]] 

No, the if line.rstrip(») test is intentional, I’m pretty sure. Using this allows the code to skip blank lines in the input.

The line.rstrip() returns the same text without spaces on the right. You can use that to remove whitespaces on right. You can use split() for converting a line to a list (the default argument of it is » » ).

def read_file(file_name): with open(file_name, 'r') as filename: l = [] for i in filename.readlines(): l.append([int(ii) for ii in i.rstrip().split()]) return l print(read_file("filename.txt")) 

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Html overflow position fixed

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Reading a text file into a matrix in Python

How do I numpy matrices from this text file in a compact way? I have solved it but it’s an ugly and long solution..

idx_A = read_data.find('A') matrix = [item.split() for item in read_data[idx_A:(idx_B+1)].split('\n')[:-1]] A = np.array(list(map(float, matrix[1]))) for i in range(2,len(matrix)-1): A = np.vstack([A,list(map(float, matrix[i]))]) 

did one of the below answers solve your problem. feel free to accept an answer, so other users can see a tested solution, or comment if it they don’t help.

2 Answers 2

AFAIK, Python doesn’t have a human-readable, flat file, serialisation format for multiple variables. In the future you should consider the npz format and the savez function to maintain human-readableness. Or if you can give up human-readableness, then check out pickle.

So to recover the data in the format you have, you’ll have to do a bit of manual file reading. This is what I came up with for a first pass attempt, which I don’t think is too messy:

from io import StringIO import numpy as np stateName, stateData = range(2) state = stateName allData = <> with open('data') as fp: for line in fp: #print(line.strip()) if state == stateName: currentName = line.strip() currentData = "" state = stateData else: # stateData if(line.strip()): # there some data on this line currentData += line else: #no data, so process what we have dataAsFile = StringIO(currentData) allData[currentName] = np.loadtxt(dataAsFile) state = stateName #Process last variable dataAsFile = StringIO(currentData) allData[currentName] = np.loadtxt(dataAsFile) 

Running it with the data from your question in a file called ‘data’, I get this:

Источник

Turn .txt file into matrix of ints in python

Is there a quick and easy way to turn this into a matrix of int? Right now, I have accessed the file this way:

file = open(testFile, 'r') data = [] for row in file: data.append(row) 

This stores the data as an array where each line is a string. Instead of going through and converting the data types and then turning it into a matrix, is there a way I can immediately store this data in matrix form as ints as I read it in?

Both loadtxt and genfromtext worked for me. Don’t know why you’re getting NaN s. @Zhiya why don’t you add your suggestion as an answer?

3 Answers 3

Simple way of doing this is:

>>> for row in file: . data.append([int(x) for x in row.split()]) . >>> data [[4, 2, 45, 21], [0, 92, 12, 2], [345, 9, 3, 4], [1, 2, 39, 93]] 

IMO, this is the most pythonic way

text = """4 2 45 21 0 92 12 2 345 9 3 4 1 2 39 93""" [[*map(int, line.split())] for line in text.split('\n')] Out[16]: [[4, 2, 45, 21], [0, 92, 12, 2], [345, 9, 3, 4], [1, 2, 39, 93]] 

If you’re okay with having your data stored as a numpy.ndarray , you can use numpy’s genfromtext() with the dtype flag set to int :

from StringIO import StringIO import numpy as np text = """4 2 45 21 0 92 12 2 345 9 3 4 1 2 39 93""" a = np.genfromtxt(StringIO(text), dtype=int) #replace the arg with your filename print(a) #[[ 4 2 45 21] # [ 0 92 12 2] # [345 9 3 4] # [ 1 2 39 93]] 

An alternative is to use loadtxt() instead of genfromtxt() as @Zhiya pointed out in the comments.

a = np.loadtxt(StringIO(text), dtype=int) 

As per this post, both functions are basically the same except that genfromtxt() provides more options for dealing with missing data.

Источник

How to to read a matrix from a given file in Python?

Reading a matrix from a file in Python can be done using several methods. Depending on the format of the file, some methods may be more suitable than others. In this answer, we will show some of the methods you can use to read a matrix from a file in Python.

Method 1: Using Numpy’s loadtxt

Here is an example code to read a matrix from a given file using Numpy’s loadtxt function:

import numpy as np matrix = np.loadtxt('filename.txt') print(matrix)

This code will load the matrix from the file filename.txt using Numpy’s loadtxt function and store it in the variable matrix . The print function will then print the matrix to the console.

If the file contains a header row, you can use the skiprows parameter to ignore it:

import numpy as np matrix = np.loadtxt('filename.txt', skiprows=1) print(matrix)

If the file contains a delimiter other than whitespace, you can specify it using the delimiter parameter:

import numpy as np matrix = np.loadtxt('filename.txt', delimiter=',') print(matrix)

If the file contains missing values, you can specify how they are represented using the missing_values parameter:

import numpy as np matrix = np.loadtxt('filename.txt', missing_values=-999) print(matrix)

You can also specify how missing values should be handled using the filling_values parameter:

import numpy as np matrix = np.loadtxt('filename.txt', missing_values=-999, filling_values=0) print(matrix)

These are just a few examples of how to use Numpy’s loadtxt function to read a matrix from a file. For more information, you can refer to the Numpy documentation.

Method 2: Using Pandas read_csv

To read a matrix from a given file in Python using Pandas read_csv, you can follow these steps:

df = pd.read_csv('file_path.csv')
df = pd.read_csv('file_path.csv', header=None)
  1. If your matrix is separated by a different delimiter other than a comma, you can specify it using the delimiter parameter:
df = pd.read_csv('file_path.csv', delimiter='\t')
  1. If your matrix has missing values, you can specify how they are represented using the na_values parameter:
df = pd.read_csv('file_path.csv', na_values=['NA', 'N/A'])
df = pd.read_csv('file_path.csv', index_col='Index')

Here’s an example code that combines all of these steps:

import pandas as pd df = pd.read_csv('file_path.csv', header=None, delimiter='\t', na_values=['NA', 'N/A'], index_col='Index') print(df)

This code reads a matrix from a file located at ‘file_path.csv’ that is separated by tabs, has no headers, has missing values represented by ‘NA’ or ‘N/A’, and has an index column named ‘Index’. The resulting DataFrame is printed to the console.

Method 3: Using the Built-in open and readlines

To read a matrix from a given file in Python, you can use the built-in open function to open the file and readlines method to read the contents of the file line by line. Here’s how you can do it step by step:

  1. Read the contents of the file line by line using the readlines method. This will return a list of strings, where each string represents a line in the file.
  1. Split each line into a list of numbers using the split method. This will return a list of lists, where each inner list represents a row in the matrix.
matrix = [list(map(int, line.split())) for line in lines]

Here’s the complete code example:

file = open('matrix.txt', 'r') lines = file.readlines() matrix = [list(map(int, line.split())) for line in lines] file.close()

Assuming the contents of the matrix.txt file are as follows:

The resulting matrix list would be:

You can then perform any matrix operations on this list as needed.

Method 4: Using the csv module

Here is a step-by-step guide on how to read a matrix from a file using the csv module in Python:

with open('matrix.txt', 'r') as file:
 reader = csv.reader(file, delimiter=' ')
  1. Iterate through the rows of the csv reader object and convert each row into a list of integers using a list comprehension:
 matrix = [[int(num) for num in row] for row in reader]

Here is the complete code:

import csv with open('matrix.txt', 'r') as file: reader = csv.reader(file, delimiter=' ') matrix = [[int(num) for num in row] for row in reader] file.close()

Assuming the file ‘matrix.txt’ contains the following matrix:

The resulting matrix will be:

That’s it! You have successfully read a matrix from a file using the csv module in Python.

Источник

Оцените статью