Question

I am struggling to write the following python script. I have a csv file that looks like this

"SNo","Title1","Title2"
"A1-A3,A4-A5","A,B","C"
"A6-A7","X","Y"
"A8","Z","D"

The output should be a csv file that should produce

"SNo","Title1","Title2"
"A1","A,B","C"
"A2","A,B","C"
"A3","A,B","C"
"A4","A,B","C"
"A5","A,B","C"
"A6","X","Y"
"A7","X","Y"
"A8","Z","D"

I am reading the file

cols= [0,1,2]
with open('C:\\down\\1\\list.csv','rb') as f:
    reader = csv.reader(f)
    for row in reader:
        content = list(row[i] for i in cols)
        numberlist =  content[0].replace("A","").split(",")
        print numberlist[0],content[1],content[2]

but I am struggling beyond that. Please tell me a good way to approach this.

Thanks

Was it helpful?

Solution

You could try this:

output_arr = []
for row in reader:
    # Get the mapping for A ranges
    idx_map = row[0].replace("A","").split(",")
    for row_map in idx_map:
            # For each pair or #-#'s creating the min and max idxs
            mapping = [int(v) for v in row_map.split('-')]
            min_map = min(mapping)
            max_map = max(mapping)
            for idx in range(min_map,max_map+1):
                    # For each value in range min_map to max_map, set values of row.
                    output_arr.append(["A%i"%(idx),row[1],row[2]])

>>> import pprint
>>> pprint.pprint(output_arr)
[['A1', 'A,B', 'C'],
 ['A2', 'A,B', 'C'],
 ['A3', 'A,B', 'C'],
 ['A4', 'A,B', 'C'],
 ['A5', 'A,B', 'C'],
 ['A6', 'X', 'Y'],
 ['A7', 'X', 'Y'],
 ['A8', 'Z', 'D']]

This will handle anomalies like: "A3-A1"

OTHER TIPS

Assuming that data is a list of lists holding the data from your csv file:

data = [["A1-A3,A4-A5","A,B","C"],
        ["A6-A7","X","Y"],
        ["A8","Z","D"]]

for line in data:
    head, tail = line[0], line[1:]
    for range_ in head.split(","):
        try:
            from_, to = range_.split("-")
            c, n, m = from_[0], int(from_[1:]), int(to[1:])
            for i in range(n, m+1):
                print c + str(i), tail
        except:
            print range_, tail

Output:

A1 ['A,B', 'C']
A2 ['A,B', 'C']
A3 ['A,B', 'C']
A4 ['A,B', 'C']
A5 ['A,B', 'C']
A6 ['X', 'Y']
A7 ['X', 'Y']
A8 ['Z', 'D']

I guess this can be a starting point:

with open('list.csv','rb') as f, open('res.csv', 'wb') as f2:
    reader = csv.reader(f)
    writer = csv.writer(f2)
    for row in reader:
        for group in row[0].split(','):
            limits = group.split('-')
            if len(limits) == 2:
                id1, id2 = [int(x[1:]) for x in limits]
                for num in range(id1, id2+1):
                    writer.writerow(("A{}".format(num),row[1],row[2]))
            else:
                writer.writerow((group,row[1],row[2]))

You need to tweak it a little bit if you want the exact output format you posted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top