So i have been given a task to develop an employee database and store all information in a CSV file. After adding information i need usable options for removing, editing and searching said data. Its a grey area on what I can and can't import so if there's a method using no porting that would be perfect. Basically, anything just using read() and write().
However, no matter what I do I can't find help online on how to actually pull this specific data in a usable format. Below is how I'm adding it to the CSV file, but no matter what I try I end up wiping the file or just getting errors when I attempt a removal process. Hopefully, whenever I can find a way to pull this data, the edit and search functions will be just a case of logic.
(My Add Function)
def add_employee(): #THIS WORKS
EmployeeID = int(input("Please enter the employee ID here "))
EmployeeName = input("Please enter the name here ")
EmployeeDep = input("Please enter the department here ")
EmployeeDes = input("Please enter the designation here ")
EmployeeStartDate = input("Please enter the start date here in format of DD-MM-YYYY ")
EmployeeStatus = input("Please enter the status here ")
with open('database.csv', "a") as f:
employee_add = ""
employee_add += "%s," % EmployeeID
employee_add += "%s," % EmployeeName
employee_add += "%s," % EmployeeDep
employee_add += "%s," % EmployeeDes
employee_add += "%s," % EmployeeStartDate
employee_add += "%s," % EmployeeStatus
employee_add += "\n"
f.write(employee_add)
print("Added correctly")
continue_or_quit()
I recommed using pandas:
import pandas as pd
Load in your csv file to a dataframe object:
employee_df = pd.read_csv("employee_info.csv")
You can add new rows:
name = "name"
password = "password"
email = "email#domain.com"
# depends on your columns, and the order of them!
employee_df.loc[df.index.max()+1] = [name, password, email]
And then export it to csv again:
employee_df.to_csv("employee_info.csv", index=False)
I actually quite like working with csv's in python using just regular formatting.
things like:
rows = content.split("\n") # get the rows (content in this example would be result of .read()
for row in rows:
values = row.split(",") # get row values
To add a row as per your example you can use something like:
row = "{},{},{},{},{},{}\n".format(EmployeeID, EmployeeName, EmployeeDep, EmployeeDes, EmployeeStartDate, EmployeeStatus)
with open(output, "a") as f:
f.write(row)
Related
everyone. I have a Python assignment that requires me to do the following:
Download this CSV fileLinks to an external site of female Oscar winners (https://docs.google.com/document/d/1Bq2T4m7FhWVXEJlD_UGti0zrIaoRCxDfRBVPOZq89bI/edit?usp=sharing) and open it into a text editor on your computer
Add a text file to your sandbox project named OscarWinnersFemales.txt
Copy and paste several lines from the original file into your sandbox file. Make sure that you include the header.
Write a Python program that does the following:
Open the file and store the file object in a variable
Read the entire contents line by line into a list and strip away the newline character at the end of each line
Using list slicing, print lines 4 through 7 of your file
Write code that will ask the user for an actress name and then search the list to see if it is in there. If it is it will display the record and if it is not it will display Sorry not found.
Close the file
Below is the code I currently have. I've already completed the first three bullet points but I can't figure out how to implement a search function into the list. Could anyone help clarify it for me? Thanks.
f = open('OscarsWinnersFemales.txt')
f = ([x.strip("\n") for x in f.readlines()])
print(f[3:7])
Here's what I tried already but it just keeps returning failure:
def search_func():
actress = input("Enter an actress name: ")
for x in f:
if actress in f:
print("success")
else:
print("failure")
search_func()
I hate it when people use complicated commands like ([x.strip("\n") for x in f.readlines()]) so ill just use multiple lines but you can do what you like.
f = open("OscarWinnersFemales.txt")
f = f.readlines()
f.close()
data = {} # will list the actors and the data as their values
for i, d in enumerate(data):
f[i] = d.strip("\n")
try:
index, year, age, name, movie = d.split(",")
except ValueError:
index, year, age, name, movie, movie2 = d.split(",")
movie += " and " + movie2
data[name] = f"{index}-> {year}-{age} | {movie}"
print(f[3:7])
def search_actr(name):
if name in data: print(data[name])
else: print("Actress does not exist in database. Remember to use captols and their full name")
I apologize if there are any errors, I decided not to download the file but everything I wrote is based off my knowledge and testing.
I have figured it out
file = open("OscarWinnersFemales.txt","r")
OscarWinnersFemales_List = []
for line in file:
stripped_line = line.strip()
OscarWinnersFemales_List.append(stripped_line)
file.close()
print(OscarWinnersFemales_List[3:7])
print()
actress_line = 0
name = input("Enter An Actress's Name: ")
for line in OscarWinnersFemales_List:
if name in line:
actress_line = line
break
if actress_line == 0:
print("Sorry, not found.")
else:
print()
print(actress_line)
So, I'm trying to transfer rows[0,1,2,9,10] from what I've designated as "e_file" to "no_file"
When I print "data" I am given the exact information I want, I was just wondering how I should proceed with transferring this data to a corresponding CSV file?
Thank you.
e_file = '/Users/massive/Desktop//NO/hour.csv'
no_file = '/Users/massive/Desktop/NO/combined.csv'
with open(e_file,'r') as e_r:
state_code = input("Enter state code: ")
county_code = input("Enter county code: ")
station_number = input("Enter station number: ")
csv_reader_2 = csv.reader(e_r)
for row in csv_reader_2:
if row[0] == str(state_code).zfill(2) and row[1] ==str(county_code).zfill(3) and row[2] == str(station_number).zfill(4):
data = [row[0],row[1],row[2],row[9],row[10]]
print(data)
Maybe something like (I cannot test it unless you provide a working example):
with open(newFile, 'wb') as csvfile:
fwriter = csv.writer(csvfile)
for line in data:
fwriter.writerow(line)
I am new to python and am looking for guidance on the task I am working on.
I am importing a csv file that looks like the list below.
invoice_id,customer_first_name,customer_last_name,part_number,quantity,total
18031,Hank,Scorpio,367,1,2.63
54886,Max,Power,171,3,50.79
19714,Jonathan,Frink,179,2,7.93
19714,Jonathan,Frink,378,2,32.34
22268,Gil,Gunderson,165,2,47.15
87681,Lionel,Hutz,218,1,50.83
84508,Lurleen,Lumpkin,257,1,81.95
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
66648,Patty,Bouvier,203,3,70.47
I only want to print each line if its based off the criteria inputted by the user. For example, if the user inputs lname and then inputs Hutz the following would be printed out.
87681,Lionel,Hutz,218,1,50.83
34018,Lionel,Hutz,112,3,88.88
34018,Lionel,Hutz,386,3,86.04
34018,Lionel,Hutz,216,1,53.54
4 records found.
This is what I have so far...
salesdatafile= None
while True:
salesdatafilename=input("Enter the name of the file:")
try:
salesdata= open(salesdatafilename, 'r')
break
except FileNotFoundError:
print("{0} was not found".format ( salesdatafilename ))
search=input("Do you want to search by invoice id or lname? Please type id or lname: ")
idsearch=salesdata.readline()
if search== 'id':
idnumber=print(int(input('Please enter Id to search: ')))
while idsearch != '':
if idsearch== idnumber:
print(idsearch)
else:
lname=print(input('Please enter your last name: '))
while idsearch != '':
if idsearch== lname:
print(idsearch)
All that is printing out is lname or id inputted by the user.
Python has a csv module built in that you should be utilizing. Check out the below example code:
import csv
salesdatafilename = r"path/to/file.txt"
data = csv.reader(open(salesdatafilename))
last_name_to_look_for = "Lionel"
for invoice_id, customer_first_name, customer_last_name, part_number, quantity, total in data:
if customer_last_name == last_name_to_look_for:
print(invoice_id, customer_first_name, customer_last_name, part_number, quantity, total)
In your code you are comparing the entire line to the idnumber or lname. Instead you might want to try something like
if lname in idsearch:
<do something>
this will check if the lname is in the line somewhere.
For performance, I will use a generator to read the lines so that my program doesn’t crash were this to be a bigger file. So I will yield each line in that file with a generator function. Since generators return iteratirs, I will then iterate and filter every line that does not contain my search value and return only those that contain my search value.
OK I have a program that outputs a users First name, Lastname and score to a string (comma separated) by using the code:
result=','.join((strFirstName,strLastName,str(score),"\n"))
I can then write this to a CSV file using the code:
file=open("filename.csv","a")
file.write(result)
file.close()
This works fine however I want to be able to record a maximum of 3 scores for each user where it only saves their latest three scores.
I cannot work out how to achieve
Only write a new row if the user does not already exist
Update an existing user with their latest score replacing the oldest score only
Like the comment mentioned, you'd have to use a dictionary or list to track users from the csv file, then modify that dictionary/list, then write back to the csv. Here's a quick implementation of that concept:
new_details = dict()
new_details["jack jackson"] = 100
users = dict()
with open("filename.csv", "r") as csv_file:
for line in csv_file:
line = line.strip().split(",")
users[line[0]+" "+line[1]] = line[2:]
with open("filename.csv", "w") as csv_file:
for user in users:
user_details = user.split()
if (user in new_details):
if len(users[user]) >= 3:
user_details += users[user][1:] + [str(new_details[user])]
else:
user_details += users[user] + [str(new_details[user])]
else:
user_details += users[user]
csv_file.write(','.join(user_details) + "\n")
for user in new_details:
if user not in users:
user_details = user.split() + [str(new_details[user])]
csv_file.write(','.join(user_details)+"\n")
Everything is based around dictionaries that use a "firstName lastName" key scheme. new_details would be the new score data you've gotten. It'll first write all existing users to the csv (adding in the new score if it exists), before writing the new users to the csv.
I am relatively new to Python and I am having trouble with the following.
I am trying to write a program that would read a CSV file of personal information and then display an individuals information based on entering an ID number.
It is almost working perfectly, except that when I search by id number, it is returning all of the results (rows) preceding the desired result, in addition to the result I want.
I am reading a CSV file into a dictionary. I am then naming the fields dynamically from the file based on the names from the CSV (theoretically the CSV file can contain 2 columns of data or 100, as long as there is one field named "id").
csvfile.txt looks like:
id,name,age
1,jay,35
2,jen,36
3,sam,38
4,mike,26
What I want is when I search for id "1", it returns:
"
id: 1
name: Jay
age: 35
"
...and it does.... but if I search for id "3", I am getting:
"
id: 1
name: Jay
age: 35
id: 2
name: Jen
age: 36
id: 3
name: Sam
age: 38
"
I can't figure out why it is not just returning the one row I am asking for... here is the core of the code:
def runprogram():
import csv
file = open(csvfile.txt, "r") #open my test file
reader = csv.DictReader(file, skipinitialspace=True, dialect='excel', delimiter=',')
totalfields = (len(reader.fieldnames)) #count all the fields in the files for the purpose of for looping.
result={} #new dictionary named result
resultfields = ""
i=1
for row in reader:
for i in range(1,totalfields):
resultfields = resultfields + reader.fieldnames[i] + ": " + row[reader.fieldnames[i]] + "\n"
i+1
result[row['id']] = resultfields #storing each line from the CSV into the results dictionary under the key "id"
#this was just some code so I could exit my program by typing "exit" in the input box...
idvalue=""
while idvalue != "exit":
#display the box in which to enter a id number
if idvalue =="":
message = "Enter id Number"
else:
message = result[idvalue]
#using easyGUI for my input and display boxes.
idvalue = eg.enterbox(msg=message,title='Print Results', default='', strip=True)
if idvalue:
identered = "1"#this was used for testing.
printresults(results[idvalue]) #call printresults function
else:
programmenu()
if idvalue =="exit":
exit()
def printresults(results):
output = "Information Requested:\n" + results
print(output)
Any help would be greatly appreciated!
You need to re-initialize resultfields for each row you process.
#!/usr/bin/python
import csv
def printresults(results):
print ("Information Requested:\n" + results)
file = open("/tmp/csvfile.txt", "r")
reader = csv.DictReader(file, skipinitialspace=True, dialect='excel', delimiter=',')
totalfields = (len(reader.fieldnames))
result={}
for row in reader:
resultfields = ""
for i in range(1,totalfields):
resultfields = resultfields + reader.fieldnames[i] + ": " + row[reader.fieldnames[i]] + "\n"
result[row['id']] = resultfields
idvalues = ["exit", "4"]
while 1:
idvalue = idvalues.pop() #eg.enterbox(msg=message,title='Print Results', default='', strip=True)
if idvalue == "":
message = "Enter id Number"
elif idvalue =="exit":
print "done"
exit()
else:
message = result[idvalue]
print(message)
Output now looks like:
name: mike
age: 26
done