-2

我正在尝试解析一个包含属性及其值的巨大 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)

任何帮助将不胜感激!

4

1 回答 1

1

如果顺序无关紧要,您可能可以将 adefaultdict用于此类事情:

from collections import defaultdict
orig = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
d = defaultdict(list)
for item in orig:
    k,v = item.split('=',1)
    d[k].append(v)

new = ['{0}={1}'.format(k,','.join(v)) for k,v in d.items()]
print(new)  #['a=1', 'c=3', 'b=2', 'e=7', 'd=4,5,6']

我想如果顺序确实很重要,您可以使用OrderedDict+setdefault但它确实不那么漂亮:

from collections import OrderedDict
orig = ['a=1', 'b=2', 'c=3', 'd=4', 'd=5', 'd=6', 'e=7']
d = OrderedDict()
for item in orig:
    k,v = item.split('=',1)
    d.setdefault(k,[]).append(v)

new = ['{0}={1}'.format(k,','.join(v)) for k,v in d.items()]
print new # ['a=1', 'b=2', 'c=3', 'd=4,5,6', 'e=7']
于 2013-03-22T16:47:35.837 回答