0

Unsolved part still

I am trying to convert the stored data in my csv file 'store.txt' into a list form so that i can use it more efficiently and easily.Like for now the items in my file store are as follows:

Leroy,55349234,ok@gmail.com  
Satish,231355,no@hi.com  
Krut,6969,69@96.com  

I want to get the above data and make it into a list like (that's in choice 4):

['Leroy','55349234','ok@gmail.com']  
['Satish','231355','no@hi.com']  
['Krut','6969','69@96.com']

I am trying to convert this into list so basically what i want to do is when the user wants to know details about a particular member , say 'Satish' , i want the code to be made such that it finds for the name 'Satish' in the whole file and then makes that whole row as a list eg:
['Satish','231355','no@hi.com']
So now the user can enter if they want to know his name,contact details or email id.Say the user wants to know his email only , so all i have to do is print element[2].

Solved

as suggested by Jura.

Now , coming to my choice 3 , (here is the code):

#a=(raw_input("Enter the staff's name: ")).capitalize()
    a=(raw_input("Enter the staff's name: ")).capitalize()
    with open('store.txt', 'r') as store:
        reader = csv.reader(store)
        for row in reader:
            if row[0]==a:
                print row
                continue
            else:
                print "No staff with name %s is found in the database,please try again."%a
                break

When i run the program and go for choice 3 , i get two bugs:
1) If i put name as 'Leroy' i get:

Database
1)Register people
2)View People registered
3)Staff Details
Enter your choice over here: 3
Enter the staff's name: leroy
['Leroy', '55349234', 'ok@gmail.com']
No staff with name Leroy is found in the database,please try again

But this is not supposed to happen as there is 'Leroy' already in the file 'store.txt' , then why does the python also run the else statement and print "No staff with name Leroy is found in the database,please try again".
2)Now when i tpye in 'Krut' it says "No staff with name Krut is found in the database,please try again." even though i have Krut as a member in my file 'store.txt' .

Here is my source code:

import csv
print "Database"
print "1)Register people"
print "2)View People registered"
print "3)Staff Details"
choice=input("Enter your choice over here: ")
#This part takes the input from the user and then stores it in 'store.txt'
if choice==1:
    a=input("Enter how many people to register: ")
    for i in range(0,a) :
        a=(raw_input("Enter the name: ")).capitalize()
        b=input("Enter contact number: ")
        c=raw_input("Enter the email ID: ")
        d=raw_input("Enter your address: ")

        with open ('store.txt','a') as store:
            storeWriter=csv.writer(store)
            storeWriter.writerow([a,b,c])
        store.close()
#This prints all the data in the file 'store.txt
if choice==2:
     with open('store.txt', 'r') as store:
        reader = csv.reader(store)
        print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - "
        for row in reader:
            print 'Name:- '+row[0]
            print 'Phone Number:- '+row[1]
            print 'Email ID:- '+row[2]
            print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - "
if choice==3:
    #a=(raw_input("Enter the staff's name: ")).capitalize()
    a=(raw_input("Enter the staff's name: ")).capitalize()
    with open('store.txt', 'r') as store:
        reader = csv.reader(store)
        for row in reader:
            if row[0]==a:
                print row
                continue
            else:
                print "No staff with name %s is found in the database,please try again."%a
                break
if choice==4:
    print "List"
    '''
    What this program does basically is that it checks the entered name(a).But then if i type the name 'Leroy' it not only prints the
    first line but also prints "No staff with name Leroy is found in the database,please try again." which is not required.And now if i
    eneter a as Krut, it prints "No staff with name %s is found in the database,please try again." even though there is a name Krut in the
    field.How do i get this done (i.e Enter Krut and then it should print row of 'Krut' that is "Krut,5537590,froost1999@outlook.com"
    Also next i want is that (in choice 4) if they eneter the name , it converts the file's(store.txt) data into a listSo basically what
    i can do with that would be like print elemenet [0] for name, element[1] for contact number,element[2] for email ID etc.Like it should
    make the first line of code that is "Leroy,55349234,ok@gmail.com" into a list form ['Leroy','55349234','ok@gmail.com']
    so that when the user asks for the contact detail of the specific person i can just print list.(1) to get
    their phone number.
    '''
White Shadow
  • 444
  • 2
  • 10
  • 26

2 Answers2

1

Try this code:

if choice==3:
    a=(raw_input("Enter the staff's name: ")).capitalize()
    with open('store.txt', 'r') as store:
        reader = csv.reader(store)
        found = False
        for row in reader:
            if row[0]==a:
                print row
                found = True
                break
        if not found:
          print "No staff with name %s is found in the database,please try again."%a
Jura
  • 49
  • 5
0

Your bug for 1) is due to your comparison: you are testing if

'Leroy' == 'leroy'

and this is not true, since the first statement is with a capital L.

For your input being read in as list, you can do something like this:

a = []
with open(file, 'r') as file:
    for line in file:
        tmp = line.replace('\n','')
        tmp = tmp.split(',')
        a.append(tmp)

name = 'Leroy'
for i in range(len(a)):
    if name == a[i][0]:
        print('found it')

This gives you:

[['Leroy', '55349234', 'ok@gmail.com'], ['Satish', '231355', 'no@hi.com'], ['Krut', '6969', '69@96.com']]
Patrick K.
  • 14
  • 3
  • I forgot to mention: the `string.split` method creates a list of strings. – Patrick K. May 10 '16 at 08:42
  • Can you be more specific about the output it gives? – White Shadow May 12 '16 at 16:20
  • in the for loop the variable tmp looks, for example, like this: `'Leroy,55349234,ok@gmail.com\n'`. With `tmp.split(',')` you split this single string on every occurance of ','. The result is: `['Leroy', '55349234', 'ok@gmail.com']` – Patrick K. May 17 '16 at 12:52