1

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
============================
4
  • 2
    1. row is a list in your code, it isn't a dict. You can't use list to do this, but I think you can do this use dict as I said. And your file example format seems broken. 2. What's the error you're getting? 3. Here's a module called csv. Commented Jan 7, 2016 at 1:32
  • Post the full error you are getting. Commented Jan 7, 2016 at 1:41
  • Okay, updated this question Commented Jan 7, 2016 at 1:49
  • 1
    Surprise: You split on a comma but there are no commas in the CSV file? Commented Jan 7, 2016 at 1:54

1 Answer 1

1

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 []
Sign up to request clarification or add additional context in comments.

1 Comment

If this is Py3 (possible, given the parenthesized prints), you'd want to change the open from open(DIR, 'rb') to open(DIR, newline='') (leaving the mode the default 'r' so you read/decode str, not bytes, but explicitly disabling line ending conversions, so csv can handle them properly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.