Benzy
Benzy

Reputation: 9

How to create matrix from table in .txt file

I need to create a matrix from a text file. the "delimiter" are not constant, sometimes it is 5 spaces and sometimes more or less. dose any one know how to do it?

I just have the lines separated, by using this code:

from tkinter import filedialog

# getting file loction and name from user:
file_path = 'file_path'
file = open(file_path) # open the file
lines = file.readlines()

enter image description here

I expect the output of matrix with 5 rows and 3 columns.

Upvotes: 0

Views: 208

Answers (1)

avloss
avloss

Reputation: 2626

try following:

import pandas as pd

file_path = 'file_path'

lines = pd.read_csv(file_path, sep=r'\s+')

Upvotes: 1

Related Questions