python - python从文件中读取二维列表中的多行到一维
问题描述
我有如下所示的文本文件:
Cat
Beth
12
female
Dog
Bob
10
male
...
..
.
我想这样存储它:
myList = [
[“Cat”,“Beth”,“12”,“female”],
[“Dog”,“Bob”,“10”,“male”],...
]
我想出了这个主意。
我的问题:有没有更短的方法来做到这一点?
with open("sample.txt") as myFile:
myList = []
temp = []
bit = 1
for line in myFile:
temp.append(line.strip())
if bit % 4 == 0:
myList.append(temp)
temp = []
bit += 1
解决方案
推荐阅读
- python - 通过 cron 作业的 TensorFlow 负载模型
- c# - 为什么属性成员需要公开?
- javascript - Mongoose 填充非规范化数据
- amazon-web-services - pem 的 rsa 公钥
- json - 减少elasticsearch聚合的结果:对象数组到简单的数字数组
- powerbi - 如何使用两种产品的订单的订单代码创建表格(或列表)
- powershell - 测试函数是否不返回任何 powershell 并返回“未找到结果”
- css - 动态地将类应用于角度自定义表单组件
- java - 从 Code Docusign API SDK Java 中完成模板字段
- angular - 使用 Formly 动态添加字段