简体   繁体   中英

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()

在此处输入图片说明

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

try following:

import pandas as pd

file_path = 'file_path'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM