Python Forum

Full Version: This is a pain, is there a quicker way to do this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
Suppose you are using python code to enter cell values into a spreadsheet like so:

sheet['A1'].value = '01' sheet['A2'].value = '02' sheet['A3'].value = '03' sheet['A4'].value = '04' sheet['A5'].value = '05' sheet['A6'].value = '06' sheet['A7'].value = '07' sheet['A8'].value = '08' sheet['A9'].value = '09' sheet['A10'].value = '10'
Then you realize cell 'A2' actually needs a value of '01a' inserted and all the other cells need to drop down one number, so A2 becomes A3, A3 becomes A4, etc.
I've been changing the A-numbers manually and I realize I'm missing out on the basic power of Python by not entering these values using a list and than iterating over the list within the cell. (I'm a new programmer and not feeling so smart so take it easy Naughty Tongue )
sheet['A1 + increment'].value = 'LIST ITERATION'
Would this be the way to do it (which would mean a lot of refactoring for me), or is there a way to quickly increment the A1, A2, A3, etc. within the python code itself?

Thanks for looking this over. I have a hunch I'm wasting a lot of time with this. Huh
Phil
Pray
I suggest a function
def update_vertically(sheet, col, row, items): row = int(row) for i, value in enumerate(items): sheet['{}{}'.format(col, row+i)].value = value update_vertically(sheet, 'A', 2, ['0{}'.format(i) for i in range(3, 11)])
Also I don't know which module you are using to create the spreadsheet, but it may have a builtin way of "updating vertically"...
Thanks Gribouillis <:

I've copied this code into my IDE and I'm going to dissect each bit to learn it. Interesting to me is implementing this will run a function within a function because the values are called from within a function.

I'll post again what I learn and any successes.

Thanks again for the direction.
Phil
I'm still not having luck with a list of strings and writing those strings into a spreadsheet column.

Phil
Do you have an error message?
Hi Gribouillis,

I was getting two consistent errors but I think I worked those two out. Now I'm not getting an error but I'm also not printing into the SS. I'm wondering if I should be using the .append method or a for-in loop instead of the enumerate function as I only have one list and don't need indexing. I'm new so my code-troubleshooting skills are more shotgunning than straight-line thinking. What's your thoughts?

Here's my code. The 'for v in values' is just to print to terminal to prove the output. I played around with different ways to work the code, but without success. I left it like this yesterday simply because it was not resulting in an error.

values = ['Mechanical / Chill Water Units Room continued', 'DP 3 Breakers', 'DP3-B08 SPD All 3 Green lights (Protected)', 'DP3-B12 CHILLER 3 Breaker is', 'DP3-B13 ATS-HPC-C Breaker is', 'DP3-B14 ATS-HPC-H Breaker is', 'DP3-B15 MUA 4 Breaker is', 'DP3-B17 ATS-HPD-H Breaker is', 'Notes'] for v in values: print(v) # Original Code from Python-forum.io: def update_vertically(sheet, col, row, items): row = int(row) for i, value in enumerate(items): sheet['{}{}'.format(col, row+i)].value = value update_vertically(sheet, 'A', 2, ['0{}'.format(i) for i in range(3, 11)])
Thanks,
Phil
Try this perhaps
update_vertically(sheet, 'A', 2, values)
on it right now <:
Took a little finagling but I got it. Boy, was I over-complicating it!! Your solution is a simple and elegant one. Thank you.
Now I have a days worth of refactoring to do!!
Cheers,
Phil