Reputation: 121
If list stored in csv file below the example, does each row stored in the array?
import csv
import os
DIR = "C:/Users/Administrator/Desktop/key_list.csv"
def Customer_List(csv):
customer = open(DIR)
for line in customer:
row = []
(row['MEM_ID'],
row['MEM_SQ'],
row['X_AUTH_USER'],
row['X_AUTH_KEY'],
row['X_STORAGE_URL'])=line.split(",")
if csv == row['MEM_ID']:
customer.close()
return(row)
else:
print ("Not search for ID")
return([])
query = input("Input the your email id: ")
result = Customer_List(query)
This example alert errors code.. Why ..? Additionally update the this code & error
Input the your email id: [email protected]
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\PyDev\Pydev\Day4\uCloudStorage.py", line 32, in <module>
result = Customer_List(query)
File "C:\Users\Administrator\Desktop\PyDev\Pydev\Day4\uCloudStorage.py", line 20, in Customer_List
row['X_STORAGE_URL'])=line.split(",")
ValueError: too many values to unpack (expected 5)
To show what's in the CSV, here's some simple code and the result:
DIR = "C:/Users/Administrator/Desktop/key_list.csv"
def Customer_List():
customer = open(DIR)
for line in customer:
print (line)
result:
MEM_ID, MEM_SQ, X_AUTH_USER, X_AUTH_KEY, X_STORAGE_URL
[email protected], M100009, M100009:M100009, wreQew3u, AUTH_xxxxxx-xxxxx
[email protected], M100022, M100022:M100022, PEm6tREx, AUTH_xxxxx-xxxxx
[email protected], M100034, M100034:M100034, 3tAzEf3u, AUTH_xxxx-xxxxx
============================================================================= I edited this script..... Is it best practice ?
DIR = "C:/Users/Administrator/Desktop/key_list.csv"
DATA = csv.reader(open(DIR,"r"))
ID = input("Input the Customer EMAIL ID: ")
def cList(value):
for row in DATA:
MEM_ID = row[0]
MEM_SQ = row[1]
X_AUTH_USER = row[2]
X_AUTH_KEY = row[3]
X_STORAGE_URL = row[4]
ACCESSKEY = row[5]
ACCESSKEYID1 = row[6]
SECRETKEY1 = row[7]
ACCESSKEYID2 = row[8]
SECRETKEY2 = row[9]
if MEM_ID == value:
print(".EMAIL ID :" + MEM_ID)
print(".MID :" + MEM_SQ)
print(".PASSWORD :" + X_AUTH_KEY)
print(".AUTH_ACCOUNT :" + X_STORAGE_URL)
print(".API KEY :" + ACCESSKEY)
cList(ID)
print ("============================")
print ("1. Upload / Download Error")
print ("2. Permission Error")
print ("3. 4xx Error")
print ("4. etc... Error")
print ("============================")
Result
Input the Customer EMAIL ID: [email protected]
.EMAIL ID :[email protected]
.MID :xxxxxx
.PASSWORD :xxxxxx
.AUTH_ACCOUNT :xxxxxx-d50a-xxxx-xxxbc05-6267d5ff6712
.API KEY :xxxxxxxx
============================
1. Upload / Download Error
2. Permission Error
3. 4xx Error
4. etc... Error
============================
Upvotes: 1
Views: 435
Reputation: 123521
If your input data is formatted like what you added at the very end of your question, your could get your approach to work like this:
import csv
DIR = "C:/Users/Administrator/Desktop/key_list.csv"
def Customer_List(email_id):
with open(DIR, newline='') as f: # open assuming Python 3.x
csvreader = csv.reader(f, skipinitialspace=True)
for fields in csvreader:
row = {} # initialize to an empty dictionary
(row['MEM_ID'],
row['MEM_SQ'],
row['X_AUTH_USER'],
row['X_AUTH_KEY'],
row['X_STORAGE_URL']) = fields
if row['MEM_ID'] == email_id:
return [row['MEM_ID'],
row['MEM_SQ'],
row['X_AUTH_USER'],
row['X_AUTH_KEY'],
row['X_STORAGE_URL']]
else:
print("ID not found")
return []
match = Customer_List('[email protected]')
if match:
print('found! {}'.format(match))
However you could simplify things slightly by using a csv.DictReader
to read the file which will automatically read the header line to obtain the fieldnames and then return a dictionary using them as keys for each row read:
def Customer_List(email_id):
with open(DIR, newline='') as f: # open assuming Python 3.x
csvreader = csv.DictReader(f, skipinitialspace=True)
for row in csvreader:
if row['MEM_ID'] == email_id:
return [row['MEM_ID'],
row['MEM_SQ'],
row['X_AUTH_USER'],
row['X_AUTH_KEY'],
row['X_STORAGE_URL']]
else:
print("ID not found")
return []
Upvotes: 1