基于关键字Python进行剥离和拆分

时间:2015-10-19 22:15:12

标签: python

我想根据csv文件here中的关键字“ IsValid ”进行剥离和拆分。我不明白为什么在找到dataString [1]的关键字后它没有用逗号分隔符拆分它。但是dataString [0]似乎是用正确的输出完成的。

示例数据

sampleData = 'DateTimeOfYCalibration,10/14/2015 11:13:28 AM, SpectrometerID,7a000001aa4fa22d\nYCalibration, Passed\nTemperature C ,9.1815\nPolynomial:,LC0,LC1,LC2,LC3,\nPolynomial:,0.998267689388189,-2.91917601399914E-07,3.58630729096524E-11,-8.13797132254045E-16,\nPixelIndex,Average Counts (Electric Dark Corrected),Average Counts Per Time,Max Counts Per Time,Standard Counts Per Time,Normalized Average Counts Per Time, Polynomial Index,Average Rate Below 3000, Fx, Residual,Smoothed residual,IsValid\n352,634.070221066319,121.91875734244,122.105500181174,121.895814648232,1.00018821560259,0,0.996183096974646,0.998096804243843,0.00209141135874924,0.000692356299613788,Valid,\n352,972.314,121.866186820435,122.105500181174,121.895814648232,0.999756941385704,0,0.996183096974646,0.998017010511045,0.00173993087465907,3.60232323325147E-06,Vali,\n919,28898.6315789474,140.969800979983,141.009749649689,140.915306078955,1.00038672094995,0,0.999291733917941,1.00014173653422,0.000244984415730176,0.000109783646710659,Valid,\n'

输入

dataString = sampleData.strip().split("IsValid")

获取dataString [0]

的输出
In [39]: dataString[0]
Out[39]: 'DateTimeOfYCalibration,10/14/2015 11:13:28 AM, SpectrometerID,7a000001aa4fa22d\nYCalibration, Passed\nTemperature C ,9.1815\nPolynomial:,LC0,LC1,LC2,LC3,\nPolynomial:,0.998267689388189,-2.91917601399914E-07,3.58630729096524E-11,-8.13797132254045E-16,\nPixelIndex,Average Counts (Electric Dark Corrected),Average Counts Per Time,Max Counts Per Time,Standard Counts Per Time,Normalized Average Counts Per Time, Polynomial Index,Average Rate Below 3000, Fx, Residual,Smoothed residual,'

获取dataString [1]

的输出
'\n352,634.070221066319,121.91875734244,122.105500181174,121.895814648232,1.00018821560259,0,0.996183096974646,0.998096804243843,0.00209141135874924,0.000692356299613788,Valid,\n352,972.314,121.866186820435,122.105500181174,121.895814648232,0.999756941385704,0,0.996183096974646,0.998017010511045,0.00173993087465907,3.60232323325147E-06,Valid'

dataString [1]的预期输出:

'\n352,634.070221066319,121.91875734244,122.105500181174,121.895814648232,1.00018821560259,0,0.996183096974646,0.998096804243843,0.00209141135874924,0.000692356299613788,Valid,\n352,972.314,121.866186820435,122.105500181174,121.895814648232,0.999756941385704,0,0.996183096974646,0.998017010511045,0.00173993087465907,3.60232323325147E-06,Valid,'

1 个答案:

答案 0 :(得分:1)

最后一个逗号仍应包含在dataString [1]中。这是因为没有参数的strip函数应该只删除空格(不是逗号)。然后,拆分函数按“IsValid”拆分。如果你想删除任何前导或尾随逗号以及任何空格,你可以这样做(使用较短的文本示例)。

 sample=',,,,Hello,1,2,,,,,  '
 sample.strip(', ')

获取

 'Hello,1,2'