Hi I have an excel file with a similar structure as below:
Location column2 column3
1 South Africa
2
3
4
5 England
6
7
8
9 U.S
10
11
12
I am trying to write a python script that can fill out the spaces between each location with the name of the preceding location (i.e fill out the space from 2 to 4 with the South Africa as the location, 6-8 will be filled out with England as the location, etc)
I would be grateful if someone could point me in the right direction.Thanks
Ok dude, I think the answer is this dumb wrapper I made for
xlrd(or, one you write yourself!). The key is that the function reads one row at a time into a list, and that Python lists remember the order in which they were populated. The wrapper produces a dictionary which maps Excel sheet names to a list of rows on that sheet (we’re assuming one table per sheet here, you’ll have to generalize things otherwise). Each row is a dictionary whose keys are the column names.For you, I’d read in your data, and then do something like this (not tested):
There might be something cute you can do with list comprehension, but I’ve had too much wine to fool with that tonight 🙂
Here’s the wrapper for the reader:
The writer is here:
Anyway, I really hope this works for you!