简体   繁体   中英

Sort excel worksheet using python

I have an excel sheet like this:

http://i.stack.imgur.com/F1807.png

I would like to output the data into an excel file like this:

http://i.stack.imgur.com/K8tWY.png

Basically, for the common elements in column 2,3,4, I want to contacenate the values in the 5th column.

Please suggest, how could I do this ?

The easiest way to approach an issue like this is exporting the spreadsheet to CSV first, in order to help ensure that it imports correctly into Python.

Using a defaultdict, you can create a dictionary that has unique keys and then iterate through lines adding the final column's values to a list.

Finally you can write it back out to a CSV format:

from collections import defaultdict

results = defaultdict(list)

with open("in_file.csv") as f:
    header = f.readline()
    for line in f:
        cols = line.split(",")
        key = ",".join(cols[0:4])
        results[key].append(cols[4])

with open("out_file.csv", "w") as f:
    f.write(header)
    for k, v in results.iteritems():
        line = '{},"{}",\n'.format(k, ", ".join(v))
        f.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM