在 Python 中,我正在嘗試創建一個登錄頁面,該頁面從 Google 表格中提取存盤的資料以用于Streamlit Authenticator。Streamlit Authenticator 通過使用用戶名、密碼和螢屏名稱訪問串列容器來驗證登錄,如下所示:
username = ['johndoe', 'janedoe']
password = ['123', '456']
names = ['John Doe', 'Jane Doe']
我正在開發的應用程式在云上運行,我將用戶資料存盤在 Google 表格中,結構如下:
username, password, names
johndoe, 123, John Doe
janedoe, 456, Jane Doe
使用pysheets和get_as_df操作,我正在尋求提取資料以填充 Streamlit Authenticator 的容器。但是,當我使用 pysheets 拉取 a 時,單個單元格會作為單獨的串列回傳,如該username列的示例所示:
import pygsheets
import pandas as pd
gc = pygsheets.authorize(service_file=local_file) #for Google Sheets API authentication
sh = gc.open('users') # Google Sheet name
wks = sh0[0] # Worksheet number
database_length = wks0.get_all_values(include_tailing_empty_rows=False, include_tailing_empty=False, returnas='matrix') # for finding the number of filled rows in sheet
end_row = str(len(database_length))
usernames_grab = wks0.get_as_df(has_header=False, index_column=0, start='A2', end=('A' end_row0), numerize=False) # grabs cell data as dataframe
usernames_list = usernames_grab.values.tolist() # converts dataframe to list
print(usernames_list)
回傳:
[['johndoe'], ['janedoe']]
然后我嘗試相應地設定 Streamlit Authenticator:
username = usernames_list
passwords = password_list
names - names_list
但是在加載腳本時,我收到以下錯誤:AttributeError: 'list' object has no attribute 'encode'我猜我不能以這種方式傳遞串列串列?關于如何進行的任何建議?
uj5u.com熱心網友回復:
這是因為您username是串列串列,而不是字串串列。
您需要做的就是從內部串列中提取字串。
>>> username = [lst[0] for lst in usernames_list]
['johndoe', 'janedoe']
如果您的usernames_grab資料框有列名,您也可以這樣做:
username = usernames_grab["username"].to_list()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453673.html
