从CSV读取,将列表值与CSV列进行比较

2024-07-04 05:48:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个房间预订系统。我的程序的第一部分接收用户输入的日期、课时和班级大小。你知道吗

该类的大小是采取和比较一个CSV文件,输出在一个列表,房间,该类可以容纳-这是正确的工作。你知道吗

我的代码的下一部分将获取日期和课时,在bookings CSV中查找,如果用户输入的日期(他们希望预订的日期)和输入的课时(他们希望预订的时间)与bookings CSV中的'date'/'lesson period'列匹配,它将查找这些房间,然后删除这些已预订的房间,从建议的房间列表,到他们实际可以预订的房间。到目前为止,我的代码是:

check_date = "12/01/2019"
check_period = "Lesson 3"

check_room = "G11"

list1 = listSuitableRooms
print(list1)

with open("Bookings.csv") as f:
    reader = csv.reader(f)
    header = next(reader)

for line in reader:
    for i in range(1, len(list1)):
        if line[2] == check_date and line[4] == check_period:
            if list1[i] == line[6]:
                print("bookings:")
                print(line)
                print()
                print("rooms that fit are:")
                list1.remove(list1[i])
                print(list1)


            else:
                print("rooms available are:")
                print(list1)

但它忽略了名单上的条件,没有删除繁忙的房间。最好不使用熊猫-我只需要导入CSV。你知道吗


Tags: csv代码用户列表fordatecheckline
1条回答
网友
1楼 · 发布于 2024-07-04 05:48:04

列名中可能有空格,范围应从0开始。你知道吗

with open("/tmp/Bookings.csv") as f: 
    reader = csv.reader(f) 
    header = next(reader) 

    for line in reader: 
        for i in range(0, len(list1)): 
            if line[2].strip() == check_date and line[4].strip() == check_period: 
                if list1[i] == line[6].strip(): 
                    print("bookings:") 
                    print(line) 
                    print() 
                    print("rooms that fit are:") 
                    list1.remove(list1[i]) 
                    print(list1) 
                else: 
                    print("rooms available are:") 
                    print(list1) 

相关问题 更多 >

    热门问题