我试图解析包含属性及其值的巨大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)
任何帮助将非常感谢!
当有重复的键*和*值时会发生什么?说'['a = 7','a = 7']'?是否应该合并为[['a = 7,7']'?订单应该保存吗? – 2013-03-22 16:46:17
技术上这不会发生。但是,如果这样做,重复可以被忽略。顺序很重要,应该保留。 – user2199889 2013-03-22 17:14:09