def register(string):
userpass = string.split(" ")
k7 = userpass[0]
k2 = userpass[1]
users.append[k7]
passes.append[k2]
return "Registered!"
不要擔心這部分^^^它只是一個我很快會使用的未使用的函式。
users = []
passes = []
csvfile = open('upassword.csv', 'r', encoding= 'utf-8')
for i in csvfile:
x = i.split(',')
if x[0] != 'username':
print('username: ', x[0])
users.append(x[0])
if x[1] != 'password':
print('password:', x[1])
passes.append(x[1])
所以在這里,我列印串列只是為了測驗值是如何從 csv 檔案中出來的,當我將它們附加并列印到代碼中的某些串列時,第一個密碼在其資料末尾回傳 '\n'由于某種原因我無法洗掉的字串。如何洗掉那個 '\n' 部分?
print(users, passes)
uj5u.com熱心網友回復:
您可以使用strip洗掉不必要的空格\ns 和\ts:
users = []
passes = []
csvfile = open('upassword.csv', 'r', encoding= 'utf-8')
for i in csvfile:
i = i.strip()
x = i.split(',')
if x[0] != 'username':
print('username: ', x[0])
users.append(x[0])
if x[1] != 'password':
print('password:', x[1])
passes.append(x[1])
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/382275.html
上一篇:找到矩陣中總和最大的列并列印出來
下一篇:無法切片陣列
