【发布时间】:2015-08-02 07:41:22
【问题描述】:
我有一个看起来像这样的 CSV:
High_Female;18 to 28 cm;25 to 30 cm;52 to 62 cm
Weight_Male;8 to 10 Kg;4 to 6 Kg;28 to 32 Kg
我一般用这些函数把它转换成数组:
def parse_csv(content, delimiter = ';'):
csv_data = []
for line in content.split('\n'):
# strips spaces also
csv_data.append([x.strip() for x in line.split(delimiter )])
return csv_data
我希望能够把事情变成:
High Female Max ; 28 ; 30; 62
High female Min ; 18 ; 25 ; 52
Weight Min ; 8 ; 4 ; 28
Weight Max ; 10 ; 6 ; 32
我只需要改造一下:
x to y unit
成一对 [x,y] 并取最小值和最大值并将它们放入一个数组中。
- 将“单位”替换为“”
list.replace("unit","") - 将“to”替换为“”
- 然后呢?使用
.split将字符串转换为列表
或者有什么更有效的方法?
【问题讨论】: