-1

I'm struggling with some project work and thought someone may be able to help me here. I am writing a database tool, and need to be able to find an ID value from a person on a csv file, and edit their employee status from active to inactive. Having very basic python knowledge, I am struggling with this. Here is my code that is working now.

def menu():
  print("Welcome to the Employee database\n")
  print("1. Add a new employee\n")
  print("2. Remove an existing employee\n")
  print("3. Change/modify the details of an existing employee\n")
  print("4. Search and display the details for a particular employee\n")
  print("0. Quit\n")

  choice = int(input("Please select an option using the numbers above"))
  if choice == 0:
     return
  if choice == 1:
     add()
  if choice == 2:
     remove()
  if choice == 3:
     change()
  if choice == 4:
     search()

def add():
  print("Adding a new employee, please enter the following details\n")
  employee_id = int(input("Enter your employee ID\n"))
  name = input("Enter your name\n")
  department = input("Enter your department\n")
  designation = input("Enter your designation\n")
  joining_date = input("Enter your starting date [DD/MM/YYYY]\n")
  employee_status = input("Enter the employee status [Active/Inactive]\n")

  with open('database.csv', "a") as f:
     employee_info = ""
     employee_info += "%s," % employee_id
     employee_info += "%s," % name
     employee_info += "%s," % department
     employee_info += "%s," % designation
     employee_info += "%s," % joining_date
     employee_info += "%s," % employee_status
     employee_info += "\n"
     f.write(employee_info)
 menu()

I need it to be able to take an input from the user of the ID they want to make inactive, find the right person in the csv and change their tag to inactive. Is this possible?

I would appreciate any help whatsoever!

Edit:

Here is the csv file contents:

Employee ID,Name,Department,Designation,Joining Date,Employee Status
456,Matt,IT,Data,02/04/2018,Active
245,John,HR,Manager,30/04/2019,Active
bumblebee
  • 1,811
  • 12
  • 19

4 Answers4

0

Just my two cents. I guess, using an ini file would be easier to use.

[ID4711]
name = Person A
status = ACTIVE

[ID4712]
name = Person B
status = ACTIVE

[ID4713]
name = Person C
status = INACTIVE

Code

import configparser

config = configparser.ConfigParser()
config.read('default.ini')
print(config['ID4711']['NAME'])
print(config['ID4711']['STATUS'])

config['ID4711']['STATUS'] = 'TEST'

with open('default.ini', 'w') as configfile:
    config.write(configfile)
Xenobiologist
  • 2,091
  • 1
  • 12
  • 16
0

You can use pandas library.

First you need to import pandas and read the csv file.

import pandas as pd
df = pd.read('/path/to/csv_file')

Since every Employee will be having a unique Employee ID, you can use loc method to change the value.

df.loc[df["Employee ID"]==12345, "Employee Status"] = "Inactive"

After doing the changes, save it back to the csv file.

df.to_csv("/path/to/csv_file", index=False)

Check this stack Change specific value in CSV file via Python

bumblebee
  • 1,811
  • 12
  • 19
0

A csv file is a sequential text file. It is a very nice interchange format, because it does not depend of any physical system. But changing one value in it ends in changing a value in a text file. And except if you are sure that the new value will have the exact same size as the original one (and Active and Inactive have different sizes...), the only foolproof way is to rewrite the whole file.

You can either load everything in memory, or copy to a temp file changing the specific line and then rename the temp file to the original name. You should find examples for that in SO:

BTW, if it is a csv file, using the csv module could be usefull...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The easiest way:

import csv

def menu():
  print("Welcome to the Employee database\n")
  print("1. Add a new employee\n")
  print("2. Remove an existing employee\n")
  print("3. Change/modify the details of an existing employee\n")
  print("4. Search and display the details for a particular employee\n")
  print("5. Inactive Employee\n")
  print("0. Quit\n")

  choice = int(input("Please select an option using the numbers above"))
  if choice == 0:
     return
  if choice == 1:
     add()
  if choice == 2:
     remove()
  if choice == 3:
     change()
  if choice == 4:
     search()
  if choice == 5:
     inactive()

def add():
  print("Adding a new employee, please enter the following details\n")
  employee_id = int(input("Enter your employee ID\n"))
  name = input("Enter your name\n")
  department = input("Enter your department\n")
  designation = input("Enter your designation\n")
  joining_date = input("Enter your starting date [DD/MM/YYYY]\n")
  employee_status = input("Enter the employee status [Active/Inactive]\n")

  with open('database.csv', "a") as f:
     employee_info = ""
     employee_info += "%s," % employee_id
     employee_info += "%s," % name
     employee_info += "%s," % department
     employee_info += "%s," % designation
     employee_info += "%s," % joining_date
     employee_info += "%s," % employee_status
     employee_info += "\n"
     f.write(employee_info)


def inactive():
    print("Inactive Employee\n")
     employee_to_inactive = int(input("Enter your employee ID\n"))

     with open('database.csv', "r") as csv_file:
         csv_reader = csv.reader(csv_file, delimiter=',')
         full_list = list(csv_reader)

         rows = []
         rows.append(full_list[0])
         for item in range(1, len(full_list)):
             if int(full_list[item][0]) == employee_to_inactive:
                 full_list[item][5] = "Inactivate"
             rows.append(full_list[item])

     with open('database.csv', 'w') as f:
         writer = csv.writer(f, delimiter=',')
         writer.writerows(rows)



menu()
Camile
  • 107
  • 5
  • hey, thanks for your help! This already seems a lot more simpler and at my level of understanding. One thing, I tried running the code and got an error when inputting an ID. It was "ValueError: invalid literal for int() with base 10: 'Employee ID'" – Matt Goody Mar 13 '19 at 11:21
  • Try it again. I changed it. – Camile Mar 13 '19 at 11:25
  • thank you so much!! works exactly as I wanted, I really appreciate it! – Matt Goody Mar 13 '19 at 11:31