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.