Python Forum
write new function or change the old one to work "smartter?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
write new function or change the old one to work "smartter?
#1
Hello,
I have created a code that update data into my DB according to my inputs

def MainPage():
    ID= input('What is the ID?\n\r')
    OldCom = input('What is the old company ID?(1, 2, 3, 4\n\r')
    NewCom = input('What is the new company ID?\n\r')
    try:
        ChangeCompany(ID, OldCom, NewCom)
    except Exception as e:
        print(e)
this works with out any problems, when I enter the 3 values and it's update the DB successfully.

My question is that I want to make it "smarter" and be able to take data from Excel file.
So I can run it automatic for 150 IDs , but also to leave the option for manually update(if I will have 1 or 2, no need for excel file)

so I have this part:
if __name__ == '__main__':
   
    while True:
        select = input('what to do? \n\r 1. Update ID\n\r 2. Update ID from Excel File \n\r 9. Exit \n\r')
        if select == "1":
            MainPage()
        if select == "2":
            wb_obj = openpyxl.load_workbook(xlsx_file)
            sheet = wb_obj.active
            print('you have ', str(sheet.max_row), ' lines of data')
            for row in sheet.iter_rows(min_row=None):
                ID = row[0].value  # this is A1\A2\A3....... for the loop 
        if select == "9":
            print('Goodbye!')
            sys.exit()
        if select not in ["1", "2", "9"]:
            print('Wrong selection, try again!')
can I use the same MainPage function ? like before ?
the changes in the excel file are the same for all the ID (all ot them have the same old_com ,and will be change to the same new_com)
or it will be easy and better to create a new function ?
Reply
#2
Hello mate,

I would recommend you look into Pandas as that can help a lot with file reading and comprehension.

You'd be best making a new function as you'd need your code to run in a specific way based on your file layout.

Can I just ask what the file looks like? Is it multiple sheets? How many columns are there? What type of database are you trying to add the data to?

cheers,
Jamie
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply
#3
Simple excel file
1 sheet, 1 column, no headline - I keep it very simple

5583680
1583808
3983809
8583810
1583811
2583812
6583813
.
.
.
I know how to work with this type of data from excel (this is no the problem)
so you say it will be better to create 2 functions? even that both of them do 95% the same job?
Reply
#4
Hello,

I wouldn't say they do 95% the same job. If I've understood correctly, the first function is for manual entry so if you're looking to keep manual entry as an option (Opposed to entry via Excel) then you'll need a second function to do the importing from the excel spreadsheet.

Cheers,
Jamie
while dad_has_cigs == True:
    happiness = True
    if dad_has_cigs == False:
    print("Dad come home!")
    happiness = not happiness
    break
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Better way to write this function SephMon 1 789 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  I dont know why my function won't work? MehHz2526 3 1,150 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  time function does not work tester_V 4 2,946 Oct-17-2021, 05:48 PM
Last Post: tester_V
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,646 Jul-17-2021, 04:19 PM
Last Post: bill_z
  string function doesn't work in script ClockPillow 3 2,333 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,377 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  Why does unpickling only work ouside of a function? pjfarley3 5 3,367 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  How to write a code with İF function? Aycaaxx 1 1,780 Nov-03-2020, 05:46 AM
Last Post: deanhystad
  Use of input function to change screen background color in Turtles Oldman45 3 4,744 Jul-10-2020, 09:54 AM
Last Post: Oldman45

Forum Jump:

User Panel Messages

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