将基于文本和空格的表转换为数组的最佳方法是什么
我从一个硬件读卡器得到以下输出。
我需要解析并获取每个单元格的值
TANK PRODUCT GALLONS INCHES WATER DEG F ULLAGE
1 REGULAR UNLEADED 87 56987 75.77 0.0 83.3 4785
2 18 4578 86.08 83.6 1661
3 SAMPLE 1234 77.94 0.0 86.4 2140
两个测试用例
- 当产品列作为空格示例“12数据”时
- 当产品列作为两个空格示例“普通无铅87”时
- 当任何单元格为空时李>
Tags:
由于单元格和数据中有空格,因此需要一点RegExp魔力:
输出:
但只有在单元格外部至少有2个空间,并且在单元格内部使用单个空间时,它才有效
短变量(相同的输出):
扩展版。它可以处理空单元格:
输出:
限制:
尝试利用
str.splitlines()
和str.split()
方法,例如:或者,使用列表理解:
相关问题 更多 >
编程相关推荐