python - Gspread Google工作表在for循环中查找行和列值
问题描述
我制作了这个程序,它将运行一个带有 Arduino 串行端口输入的 python 脚本。基本上,我的项目是一个考勤设备,人们可以使用 RFID 读卡器登录和退出。如何在列表中找到相关数据点?
我将我的电子表格的一行(例如名称列表)分配给一个变量。然后我搜索每个单元格,直到找到正确的名称。问题是我想找到我在其中找到名称的单元格列。我不知道该怎么做,但我写了一些东西作为占位符+我希望它如何在标有“参考”的行中工作第 1 点”在我的代码中。
# Credentials and imports ^^
file = gspread.authorize(credentials) # authenticate with Google
sheet = file.open("RFID-2019-2020-Attendance").sheet1 # open sheet
#placeholder
date_added = False
name_added = False
name_matched = False
current_card_number = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
def current_card_name():
for id in id_Numbers:
if id == current_card_number and name_matched == False:
matching_name = sheet.cell(id.row,1)
name_matched = True
return matching_name
def current_card_number():
card_num = serial.read_until('*', 28)
return card_num
while True:
print('Scan A Card To Sign In')
id_Numbers = sheet.col_values(2)
names = sheet.row_values(1, 1)
dates = sheet.row_values(1)
for name in names:
if name.value == current_card_name(): # if card is alread in system
print('Card Recognized...')
for date in dates:# check if date is already a headre
if date == datetime.today().strftime('%Y-%m-%d'):# date is in header
# reference point 1
sheet.update_cell(date.row,name.column, 'P')
else:
#date not in header
for date in dates:# find new open spot in header
if date == '' and date_added == False:# if date is non existant
# write date in new row
sheet.update_cell(date, 1, datetime.today().strftime('%Y-%m-%d'))
date_added = True
#write "P" in the new date column
sheet.update_cell(date.row, name.column, 'P')
date_added = False # reset variable after date is added
print('Welcome ' + name + '! You Have Been Signed In ')
else:# if card is not in system
print('Card not Recognized.')
print('1) Add new Profile')
print('2) Go back to main menu')
print('3) Change card')
cmd_input = input()
if cmd_input == '1':
print('What is the name associated with this card?')
cmd_input = input()
for name in names:
if name == '' and name_added == False:
sheet.update_cell(name.row, 1,cmd_input)# names in first column
sheet.update_cell(name.row, 2, current_card_number())#puts card ID in second columnFrea
name_added = True
print('New profile Created! Returning to main Menu...')
if cmd_input == '2':
print('Returning to main screen...')
if cmd_input == '3':
print('What is the name of the Profile associated with the card?')
cmd_input = input()
# add new card y/n
# no will take you back to main screen
我知道这是错误的,所以我没有运行它。任何帮助将不胜感激。
解决方案
推荐阅读
- python - 在 python 线程中使用时,cv2.imshow() 窗口空白?我不确定如何解决这个问题
- python - 我可以在python中附加一个范围吗
- php - PHP DateTime 问题:在布尔值上调用成员函数 format()
- python - 使用 Pytest 测试文件是否有效
- android - 如何在从活动中单击按钮时使复选框在 RecyclerView 中可见?
- python - 多个 Pandas 列表列,希望为每个列表元素展开并创建一行
- asp.net-core - 类型安全的 `a` 标记助手
- php - 获取 singleValueExtendedProperties
- encoding - 字符的编码是否取决于字体的字体样式
- java - Java 表示法 - 印刷。
toSysOut().withLabel("源流")