我已经通过将每一行读取为新元素来将文件的内容读取到列表中。现在,我想将这些进一步分为自己的子列表。例如,每个字符串中第一个逗号之前的第一个值是一个日期,因此我想创建一个仅包含此给定列表中日期的新列表,依此类推。我该怎么做呢?
['2019/01/11,1640.5600,4670093.0000,1640.5500,1660.2940,1636.2200', '2019/01/10,1656.2200,6487252.0000,1641.0100,1663.2500,1621.6150', '2019/01/09,1659.4200,6333528.0000,1652.9800,1667.7989,1641.3953', '2014/01/13,390.9800,2843810.0000,397.9800,399.7800,388.4500']
我尝试使用list.split
来分隔逗号所表示的值,但是我无法使它适用于整个列表。
答案 0 :(得分:1)
lines = [
'2019/01/11,1640.5600,4670093.0000,1640.5500,1660.2940,1636.2200',
'2019/01/10,1656.2200,6487252.0000,1641.0100,1663.2500,1621.6150',
'2019/01/09,1659.4200,6333528.0000,1652.9800,1667.7989,1641.3953',
'2014/01/13,390.9800,2843810.0000,397.9800,399.7800,388.4500',
]
dates = [line.split(',')[0] for line in lines]
print(dates)