Hey guys so i'm pretty new at python and was wondering how I can specifically search for a word/number in Python and its outcome is the row of where that word is from.
staff.txt contains rows of information like
ID Name StartDate Role
001 John 1-1-2001 Kitchenhand
002 Mike 2-1-2001 Cashier
My code so far is this. I'm able to read the txt file, make choices on how to search up a staff through ID or email. I'm just lost on how to code the bit where it searches for the ID inside the txt file. Any help or guidance would be appreciated.
import random
choices = ["1","2"]
choice2a = random.choice(choices)
# reads txt file
df = pd.read_csv('staff.txt', 'w', delimiter='\t')
# deletes Unnamed columns
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
name= input("How would you like to search for staff?")
print ("1)ID")
print ("2)Email")
choice1 = input("Type Answer")#choice
if choice1 == "1" or choice1 == "1":
input("Type staff ID")
ID = ```
Read the file into memory with the variable df
as you are then do something like this
# create a function
def myFunc(df):
# your input
name = input(f"How would you like to search for staff? \n1) ID\n2) Email\n[1/2]: ")
if name == '1': # if 1 ask for id input
ids = input('Type ID number: ') # another input
return df[df['ID'] == int(ids)] # boolean indexing to return df based on id
elif name == '2': # if 2 ask for email address
email =input('Type email address: ') # another input
return df[df['email'] == email] # boolean indexing to return df based on email address
else:
raise ValueError(f'{name} is not 1 or 2') # raise value error if you enter a value other than 1 or 2
myFunc(df) # run the function
When run it looks like
How would you like to search for staff?
1) ID
2) Email
[1/2]: 1
Type ID number: 2
ID Name StartDate Role
1 2 Mike 2-1-2001 Cashier
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.