Home > front end >  Reading file based on given option
Reading file based on given option

Time:09-17

I have a program that has multiple functions defined by

def function_1():

And so on. These are accessed through a menu defined here:

menu()
option = int(raw_input("Select an option: "))
while option != 0:
        if option == 1:
                function_1(df)
        elif option == 2:
                function_2(df)
        elif option == 3:
                function_3(df)
        elif option == 4:
                function_4(df)
        else:
                print("This option is not valid.")
        menu()
        option = int(input("Select option: "))
print("Thanks for using program.")

The menu looks like this:

def menu():
    print("[1]: 1st Function")
    print("[2]: 2nd Function")
    print("[3]: 3rd Function")
    print("[4]: 4th Function")
    print("[6]: Modify the year to analyse")
    print("[0]: Quit Program")

I have 3 files that I have are named as followed: 2000.rep.csv 2010.rep.csv 2020.rep.csv

I need to find a way for the program to ask (at the start of the program) for a year between 2000, 2010 or 2020 and for the program to read the file for that year, using something like: df = pd.read_csv('2010.rep.csv').

I also need the program to offer the option to change the year through the menu if needed (so read a different file if needed).

I have tried multiple things but I can't get any to work, I just need a bit of help as python is new to me. Any help is appreciated.

Using Python 2.7 and Pandas 0.24.2

CodePudding user response:

Make an option, just like what you did.

def select_year():
    print('2000')
    print('2010')
    print('2020')
select_year()
year = str(raw_input('Please select a year: '))

# If year is not in list
while not year in ['2000','2010','2020']:
    print('Year is not valid')
    select_year()
    year = str(raw_input('Please select a year: '))

df = pd.read_csv(year   '.rep.csv')
  • Related