2017-09-17 10 views
0

xlsx- 'saito'という名前のユーザー名を検索しようとしています(xslxに手動で追加しました)。ほとんどの回答は、特定のセルを検索するときにのみ機能します。私の意図は、私はユーザー名を検索することができますログインプログラムを作成すると、ユーザー名がファイル内に存在する場合は、その後、関連するパスワードを依頼し、まだ文字列を検索する方法を見つけることができません。以下はpython 3を使ってxlsxファイルのデータを検索するには?

counter = 0 
print('type L to log in or C to create an account') 
decision = input().lower() 
if decision == 'l': 
    while counter == 0: 
     print('please enter your user name') 
     username = input() 
     while username == '': 
      print('please enter a valid username') 
      username = input() 
     if username in Accounts.xlsx: 
      print('please enter your password') 
      counter = counter + 1 
     else: 
      print('The username you have entered has not been recognised, please try again') 
elif decision == 'c': 
    print('I'll do the user creation later') 
else: 
    print('please log in or create a user') 

Accounts.xlsxは私がユーザ名を検索するために使用されるファイルです...私の現在のコードです。

+1

この回答であるかどうかをチェック参考になるかもしれません:https://stackoverflow.com/questions/38056233/python-search-excel-sheet-for-specific-strings-in-a-column-and-extract-those-ro –

+0

あなたはパンダを学ぶことができます。 https://stackoverflow.com/questions/17063458/reading-an-excel-file-in-python-using-pandas –

答えて

0

Openpyxlは、.xlsxファイルでの作業に非常に便利です。あなただけのユーザー名は、ファイル内の任意の場所にあるかどうかを確認したい場合は、そのような値のすべてを抽出することができます。

from openpyxl import load_workbook 

wb = load_workbook(filename='Accounts.xlsx') 
sheet = wb['sheet_name'] 

names = [] 

for i in range(1, sheet.max_row + 1): 
    for j in range(sheet.max_column): 
     if sheet[i][j].value: 
      names.append(sheet[i][j].value) 

それからちょうどユーザ名がリスト

if username in names: 
関連する問題