我正在用Python编写代码(我对Python很陌生)并希望拆分一个类似于下面示例的.csv文件。我希望每个包含csv行的项目列表,然后我知道如何拆分,使用:
values = line.split(',')
弃1
0.058824,0.529032,0.367347,0.23913,0.169471,0.243354,0.115713,0.433333,yes
0.529412,0.812903,0.510204,0.26087,0.169471,0.527607,0.13877,0.366667,yes
弃2
0.176471,0.806452,0.510204,0.130435,0.133413,0.239264,0.081127,0.166667,yes
0.411765,0.632258,0.673469,0.184783,0.560096,0.249489,0.021349,0.366667,yes
弃3
0.647059,0.606452,0.510204,0.206522,0.15625,0.366053,0.204526,0.483333,yes
0.411765,0.8,0.653061,0.380435,0.36899,0.408998,0.302733,0.316667,yes
理想情况下,我们希望:
list[0] = ["0.058824,0.529032,0.367347,0.23913,0.169471,0.243354,0.115713,0.433333,yes" , "0.529412,0.812903,0.510204,0.26087,0.169471,0.527607,0.13877,0.366667,yes"]
list[1] = ["0.176471,0.806452,0.510204,0.130435,0.133413,0.239264,0.081127,0.166667,yes" , "0.411765,0.632258,0.673469,0.184783,0.560096,0.249489,0.021349,0.366667,yes"]
等等。 非常感谢您提供的任何帮助:)
答案 0 :(得分:0)
value = (line.split(' '))[-1].split(",")
首先按空格分割,然后拾取最后一个元素,然后按","试。
答案 1 :(得分:0)
list1 = []
with open (filename,'r') as file:
data = file.read()
if data != '':
list1.append(data[7:])