Python - 我怎么能简化这个?

时间:2017-08-01 00:31:46

标签: python csv

#Checking Of Class
#N BLOCK
if eqid == "N001":
    class_ = "N001"
    print ("Class selected: "+class_)
    continue

if eqid == "N002":
    class_ = "N002"
    print ("Class selected: "+class_)
    continue

if eqid == "N003":
    class_ = "N003"
    print ("Class selected: "+class_)
    continue

if eqid == "N004":
    class_ = "N004"
    print ("Class selected: "+class_)
    continue

#E BLOCK
if eqid == "E001":
    class_ = "E001"
    print ("Class selected: "+class_)
    continue

if eqid == "E002":
    class_ = "E002"
    print ("Class selected: "+class_)
    continue

if eqid == "E003":
    class_ = "E003"
    print ("Class selected: "+class_)
    continue

if eqid == "E004":
    class_ = "E004"
    print ("Class selected: "+class_)
    continue

至少还有另外6个“块”,至少还有另外30个“类”,我怎么能让这个更简单而不是那么长?我尝试过eqid == class_,但这不符合我的需要,因为eqid然后会导出到CSV文件中,我不会将任何“类”导入到CSV文件中。

eqid = str.upper(raw_input('Please swipe your card: '))

这就是我从中获取eqid的地方。

#CSV Writer
    ofile = open('Attendance.csv', "a")
    writer = csv.writer(ofile, delimiter=',')
    writer.writerow([eqid, period+1, clcode, attendance]) 
    ofile.close()

这是CSV编写器,整个脚本处于while循环中。

import csv
import datetime
import os

while (1):
    #Defaults
    class_ = "N004" #If no class is specified it is defaulted to N004

    #Magnetic Card Reader Output & Attendance (Default = 0)
    attendance = '0'
    eqid = str.upper(raw_input('Please swipe your card: '))

    #Checking Of Class
    #N BLOCK
    if eqid == "N001":
        class_ = "N001"
        print ("Class selected: "+class_)
        continue

    if eqid == "N002":
        class_ = "N002"
        print ("Class selected: "+class_)
        continue

    if eqid == "N003":
        class_ = "N003"
        print ("Class selected: "+class_)
        continue

    if eqid == "N004":
        class_ = "N004"
        print ("Class selected: "+class_)
        continue

    #E BLOCK
    if eqid == "E001":
        class_ = "E001"
        print ("Class selected: "+class_)
        continue

    if eqid == "E002":
        class_ = "E002"
        print ("Class selected: "+class_)
        continue

    if eqid == "E003":
        class_ = "E003"
        print ("Class selected: "+class_)
        continue

    if eqid == "E004":
        class_ = "E004"
        print ("Class selected: "+class_)
        continue

    #Day & Time Checker
    format = "%H%M%S"
    format_ = "%H%M"
    today = datetime.datetime.today()
    s = today.strftime(format) #Time in 24hour
    s2 = today.strftime(format_)
    d = datetime.datetime.today().weekday() #Day of week (0-5)
    period = 0 #If they scan it outside of the dedicated periods it defaults to period 1

    #Period Checker
    if "084500" < s < "094000":
        period = 0
    if "094000" < s < "104000":
        period = 1
    if "112000" < s < "121500":
        period = 2
    if "121500" < s < "131500":
        period = 3
    if "133500" < s < "143000":
        period = 4

    #Class Code Reader
    dataList = []
    with open('J:/attendance/Timetables/'+class_+'.csv', 'r') as csvfile:
      csvreader = csv.reader(csvfile)
      for row in csvreader:
         dataList.append(row)
    csvfile.close()

    #Class Code
    clcode = dataList[period][d] 

    #CSV Writer
    ofile = open('Attendance.csv', "a")
    writer = csv.writer(ofile, delimiter=',')
    writer.writerow([eqid, period+1, clcode, attendance]) 
    ofile.close()

TL; DR:我将如何简化我的Checking Of Class脚本。

谢谢

2 个答案:

答案 0 :(得分:1)

看起来您只是检查eqid是否为允许的类值?

尝试

allowed_classes = set(["N001", "N002", "N003", "N004", "E001", "E002", "E003"])  # etc
default_class = "N004"

while True:
    eqid = raw_input('Please swipe your card: ').strip().upper()
    class_ = eqid if eqid in allowed_classes else default_class
    print("Selected class", class_)
    # rest of processing...

此外,“类代码阅读器”块不应该在你的循环中(现在你正在为你处理的每个学生重新加载一个类文件);而是像

一样尝试阅读它们
class_timetable_file = "J:/attendance/Timetables/{class_}.csv"

# class_codes[class_][period][day_of_week] = class_code
class_codes = {}

for class_ in allowed_classes:
    with open(class_timetable_file.format(class_ = class_)) as inf:
        rd = csv.reader(inf)
        class_codes[class_] = list(rd)   # list of list of str

答案 1 :(得分:0)

我猜你不需要以下

#N BLOCK
    if eqid == "N001":
        class_ = "N001"
        print ("Class selected: "+class_)
        continue

    if eqid == "N002":
        class_ = "N002"
        print ("Class selected: "+class_)
        continue

    if eqid == "N003":
        class_ = "N003"
        print ("Class selected: "+class_)
        continue

    if eqid == "N004":
        class_ = "N004"
        print ("Class selected: "+class_)
        continue

    #E BLOCK
    if eqid == "E001":
        class_ = "E001"
        print ("Class selected: "+class_)
        continue

    if eqid == "E002":
        class_ = "E002"
        print ("Class selected: "+class_)
        continue

    if eqid == "E003":
        class_ = "E003"
        print ("Class selected: "+class_)
        continue

    if eqid == "E004":
        class_ = "E004"
        print ("Class selected: "+class_)
        continue

因为您只是在检查if条件后将其分配给class_变量。我的解决方案将是&#34;您可以直接分配它 &#34;