Python Forum
This is a pain, is there a quicker way to do this?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
This is a pain, is there a quicker way to do this?
#1
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
Reply
#2
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"...
Reply
#3
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
Reply
#4
I'm still not having luck with a list of strings and writing those strings into a spreadsheet column.

Phil
Reply
#5
Do you have an error message?
Reply
#6
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
Reply
#7
Try this perhaps
update_vertically(sheet, 'A', 2, values)
Reply
#8
on it right now <:
Reply
#9
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  3 easy questions for python programmers but 3 pain in the ass for me grjmmr 6 3,910 Jul-22-2018, 08:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020