python - How to save floats from txt file with multiple arrays?
问题描述
I would like to extract data from a txt file stored as:
[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
(there is no vertical space between the brackets though)
Would you have any idea how to proceed? I have tried this below which does not recognize the brackets.
with open(filename) as f:
array = []
for line in f:
array.append([float(x) for x in line.split()])
解决方案
假设括号中的每个子列表都存储在新行上。
替换line.split()
为:
line.strip()[1:-1].split()
这只是用 . 去掉了两个括号(或第一个和最后一个字符)[1:-1]
。
推荐阅读
- github - 从 Azure Pipeline 创建 GitHub PR 评论
- java - 在 Java 中重采样音频
- php - 将允许添加到购物车的产品的最大数量设置为 6 并通知
- angular - 如何获取来自角度服务的变量
- winapi - 如何安全地杀死 robocopy 进程
- node.js - Mongoose "尚未为模型 \"Designation\" 注册架构。\n使用 mongoose.model(name, schema)"
- javascript - 使用 react-router 路由后,样式的 glidejs 动态计算被破坏
- nginx - Nginx - 从多个子目录索引静态文件夹,为 GeoIp 重写
- python - 如何同时绘制多个点云python
- python-3.x - 我可以在 Azure ML 的一次训练中使用两个计算集群吗?