我需要从文本文件创建矩阵。 “定界符”不是恒定的,有时是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()
我希望输出5行3列矩阵。
答案 0 :(得分:1)
尝试以下操作:
import pandas as pd
file_path = 'file_path'
lines = pd.read_csv(file_path, sep=r'\s+')