我有一个非常简单的title : data
格式的数据文件。
例如(实际文件相同,但包含更多数据):
Images : 50
Total cells : 532
Viable cells : 512
Nonviable cells : 20
现在,为了分析这个问题,我为我想要的每个数据段提供了以下代码:
if data[1][:12] == "Total Cells :":
result.append(data[1][13:-1])
不过,这似乎是一个非常肮脏的解决方案。解决这个问题的更干净的方法是什么?你知道吗
Tags:
你可以使用
str.split()
,但是你可以只使用str.partition()
, 以下是帮助文本:对于
partition
:对于
split
:我建议使用简易界面:
你可以用一些类似的方法:
只需在
' : '
上拆分行:现在将行的两个元素分为两个变量。您可能需要去除这些无关的空白:
演示:
如果希望将此数据文件放在一个漂亮的字典中,可以执行以下操作:
打印
d
将返回:这假设您的“键”或“值”中没有
:
。你知道吗然后可以访问任何元素(即“总单元格”),如下所示:
相关问题 更多 >
编程相关推荐