python - 如何从 .txt 文件中的表创建矩阵
问题描述
我需要从文本文件创建一个矩阵。“分隔符”不是恒定的,有时是 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 列的矩阵。
解决方案
尝试以下:
import pandas as pd
file_path = 'file_path'
lines = pd.read_csv(file_path, sep=r'\s+')
推荐阅读
- exception - HP 通用打印驱动程序 (UPD) 7.0
- google-cloud-firestore - 错误:发生网络错误(如超时、连接中断或主机无法访问)
- airflow - 哪个性能更高:创建 1000+ DAG 或 1 DAG w/1000+ 任务?
- kotlin - 如何使用 ESC/POS 设置字符集?
- mobile - 我可以使用我编译的 Nativescript 项目继续构建原生应用程序吗?
- json - 如何在 Angular 中测试定期获取 json
- java - 从 OpenAPI 规范生成代码,包括示例和描述
- android - Cannot customize button when using Theme.MaterialComponents.DayNight.NoActionBar as theme
- jwt - 石墨烯 JWT 认证
- javascript - 内嵌 PDF 到图像的转换 (js & html)