我正在尝试解析一个包含属性及其值的巨大 Excel 文件。问题如下:一些属性能够包含多个值。
例子:
list = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
应该:
list2 = ['a=1', 'b=2', 'c=3', 'd=4,5,6', 'e=7']
元素是具有可变长度的字符串,它们由“=”分隔。
这就是我从 Excel 文件中生成列表的方式:
#for each row in the excel file.
for rows in range(DATA_ROW, sheet.nrows):
#generate a list with all properties.
for cols in range(sheet.ncols):
#if the propertie is not emty
if str(sheet.cell(PROPERTIE_ROW,cols).value) is not '':
proplist.append(sheet.cell(PROPERTIE_ROW,cols).value + '=' + str(sheet.cell(rows,cols).value) + '\n')
我试了一下,但效果不是很好......
last_item = ''
new_list = []
#find and collect multiple values.
for i, item in enumerate(proplist):
#if the propertie is already in the list
if str(item).find(last_item) is not -1:
#just copy the value and append it to the propertie
new_list.insert(i, propertie);
else:
#slize the string in propertie and value
pos = item.find('=')
propertie = item[0:pos+1]
value = item[pos+1:len(item)]
#save the propertie
last_item = propertie
#append item
new_list.append(item)
任何帮助将不胜感激!