2016-05-06 8 views
0

まだ未解決の部分

私のcsvファイル 'store.txt'に保存されているデータをリストフォームに変換しようとしています。私はより効率的かつ簡単に使用できます。次のとおりです。csvファイルのデータをリストに変換するには?

Leroy,55349234,[email protected] 
Satish,231355,[email protected] 
Krut,6969,[email protected] 

私は(つまり、選択肢4にあります)上記のデータを取得するなど、リストにそれをしたい:

['Leroy','55349234','[email protected]'] 
['Satish','231355','[email protected]'] 
['Krut','6969','[email protected]'] 

私は基本的にどのようなリストにこれを変換しようとしています私はしたいときは、ユーザーが知りたいときです特定のメンバーについての詳細は、 'Satish'と言います。ファイル全体でSatishという名前が見つかるようにコードを作成し、その行全体をリストとして作成します。
['Satish' 、 '231355'、 '[email protected]']
これで、ユーザーは自分の名前、連絡先の詳細、またはメールIDを知りたい場合に入力できます。ユーザーは自分のメールのみを知りたいので、 doは という印刷要素[2]です。ジュラにより示唆されるように

を解決しました。今

、私の選択3、(ここでコードがある)に来る:私は置く場合)
1:私は、プログラムを実行すると選択肢3のために行くとき、私は2個のバグを取得

#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 

「リロイ」として名Iを得る:

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', '[email protected]'] 
No staff with name Leroy is found in the database,please try again 

しかし、これが「リロイは」ファイルにすでに存在しているとして「store.txt」起こると想定されていない、なぜPythonはまた、else文と印刷を実行しません"Leroyという名前のスタッフはデータベースには見つかりませんでした。リースをもう一度試してください "。
2)今度は「Krut」で「Krutという名前のスタッフがデータベースに見つかりませんでした。もう一度お試しください」というメッセージが表示されます。私はファイル 'store.txt'のメンバーとしてKrutを持っていますが、 です。ここで

は、私のソースコードは次のとおりです。

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,[email protected]" 
    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,[email protected]" into a list form ['Leroy','55349234','[email protected]'] 
    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. 
    ''' 
+0

はこの答えを見てくださいhttp://stackoverflow.com/a/36807361/1437877とpythonの 'pandas'モジュール – Abbas

+0

を使います。 – sb0709

答えて

1

は、このコードを試してみてください:1のため

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 
+0

ありがとう、これは私がそれがしたいと思ったように働いた。私はまだリストにそれを変換しようとしています。 –

0

あなたのバグ)によるあなたの比較にある:

'Leroy' == 'leroy' 

場合は、テストしています最初の声明は大文字のLであるので、これは真実ではない。

ご入力の場合

がリストとして読み込まれている、あなたはこのような何かを行うことができます。これはあなたを与える

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') 

[['Leroy', '55349234', '[email protected]'], ['Satish', '231355', '[email protected]'], ['Krut', '6969', '[email protected]']] 
+0

私は言及して忘れました: 'string.split'メソッドは文字列のリストを作成します。 – Patrick

+0

出力の詳細について教えてください。 –

+0

のforループでは、変数tmpは次のようになります: ''Leroy、55349234、ok @ gmail.com \ n" ' 'tmp.split( '、')'では、 '、'の出現ごとにこの単一の文字列を分割します。結果は次のとおりです: '['Leroy'、 '55349234'、 '[email protected]']' – Patrick

関連する問題