如果列表存儲在csv文件的下面,那麼每行是否存儲在數組中?將CSV文件中的數據存儲到數組中?
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)
此示例提醒錯誤代碼..爲什麼..? 此外更新該代碼&錯誤
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)
要顯示什麼的CSV,這裏有一些簡單的代碼和結果:
DIR = "C:/Users/Administrator/Desktop/key_list.csv"
def Customer_List():
customer = open(DIR)
for line in customer:
print (line)
結果:
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
==== ================================================== ======================= 我編輯了這個腳本.....是最佳實踐嗎?
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 ("============================")
結果
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
============================
1'row'是** **名單在你的代碼,它**不是字典**。你不能用list來做到這一點,但我認爲你可以像我說的那樣使用** dict **。而你的文件示例格式似乎已經損壞。 2.你得到的錯誤是什麼? 3.這是一個名爲['csv']的模塊(https://docs.python.org/3/library/csv.html#csv)。 –
發佈您正在獲取的完整錯誤。 – Gabriel
好的,更新了這個問題 –