将输入的值与csv文件中的行进行比较

时间:2015-12-03 13:18:25

标签: python

基本上,我需要检查输入的值是否与csv文件的每一行中的第四个值相同。

E.g。 :

csv文件看起来像这样:

姓名,电子邮件,电话号码,高级,部分数据,部分数据,部分数据

姓名,电子邮件,电话号码,专业版,部分数据,部分数据,部分数据

姓名,电子邮件,电话号码,套件,部分数据,部分数据,部分数据

姓名,电子邮件,电话号码,高级,部分数据,部分数据,部分数据

输入值= 高级

for循环检查csv文件中的每一行,并在值相同时执行操作。

我的代码目前看起来像是:

def readFile(aFile):
#read a file and return a list
        with open(aFile, 'r') as inFile: 
            reader = csv.reader(inFile)
            aList = [row for row in reader]
        return aList


    @app.route('/addBooking', methods = ['POST'])
    def addBooking():
        #read the booking list from file
        fileName = 'static\\bookings.csv'
        bookList = readFile(fileName)
        fileName1 = 'static\\errors.csv'
        errorList = readFile(fileName1)
        # add an entry to the bookings list
        name = (request.form[('name')])
        email = (request.form[('email')])
        phone = (request.form[('number')])
        room = (request.form[('room')])
        quantity = (request.form[('quantity')])
        inDate = (request.form[('date1')])
        outDate = (request.form[('date2')])
        #we have only 15 rooms of each type
        #so the website will show an error if an user tries to book more than 15 rooms
        if int(quantity)>15: 
            return render_template('booking.html', errorList = errorList)
        else:
            for line in bookList:
                if room == line[4]:
                    if (int(quantity)+int(line[5]))>15:
                        return render_template('booking.html', errorList = errorList)
                        break
                    else:
                        newBook=[name,email,phone,room,quantity,inDate,outDate]
                        bookList.append(newBook)
                        #save the bookings list to the file
                        writeFile(bookList, fileName)
                        return redirect('booking')
                        break
                else:
                    newBook=[name,email,phone,room,quantity,inDate,outDate]
                    bookList.append(newBook)
                    #save the bookings list to the file
                    writeFile(bookList, fileName)
                    return redirect('booking')
                    break

有人有任何想法如何使循环工作?谢谢!

0 个答案:

没有答案