如何从.txt文件中的表创建矩阵

时间:2019-05-02 08:48:16

标签: python

我需要从文本文件创建矩阵。 “定界符”不是恒定的,有时是5个空格,有时或多或少。 有人知道怎么做吗?

通过使用以下代码,我将行分隔开:

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

我希望输出5行3列矩阵。

1 个答案:

答案 0 :(得分:1)

尝试以下操作:

import pandas as pd

file_path = 'file_path'

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