如何在我的预订系统中写入我的CSV文件[STUCK]

时间:2014-12-30 14:32:08

标签: python python-2.7 csv

我知道我听起来绝对愚蠢,但我是Python新手,作为我的学校项目,我必须创建一个电影预订系统。

我已决定以格式写入CSV文件:

 ,1,2,3,4,5,6,7
A,+,+,+,+,+,+,+
B,+,+,+,-,+,+,+
C,+,+,+,-,+,+,+
D,+,+,+,-,+,+,+
E,+,+,+,-,+,+,+

'+'表示无座位

' - '表示座位不存在

'*'表示预订座位

我能否轻松阅读并检查第1列的第A行是否为免费,如果是,则将其设置为*意味着他们将被预订......

如果我有可能,如果有人能指出我正确的方向,我会非常感激。

我对其余的没有任何问题,只是我遇到了障碍,我的脑子里空白的研究和谷歌搜索时间无法解决这个问题。非常感谢帮助。

2 个答案:

答案 0 :(得分:4)

试试这个:

import csv

with open('booking_system.csv', 'w') as csvfile:
    fieldnames = [' '] + [str(x) for x in range range(1, 8)]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({' ': 'A', '1': '+', '2': '+', 
                     '3': '+', '4': '+', '5': '+',
                     '6': '+', '7': '+'})
    #And so on...

答案 1 :(得分:1)

座位应为A,1 A,2等。

from pprint import pprint as pp
missing = (("B",4),("C",4),("D",4),("E",4))

seats = {(x,y):"x" if (x,y) not in missing else "-" for x in ["A","B","C","D","E"] for y in range(1,8)}

pp(seats)
{('A', '1'): '+',
 ('A', '2'): '+',
 ('A', '3'): '+',
 ('A', '4'): '+',
 ('A', '5'): '+',
 ('A', '6'): '+',
 ('A', '7'): '+',
 ('B', '1'): '+',
 ('B', '2'): '+',
 ('B', '3'): '+',
 ('B', '4'): '-',
 ('B', '5'): '+',
 ('B', '6'): '+',
 ('B', '7'): '+',
 ('C', '1'): '+',
 ('C', '2'): '+',
 ('C', '3'): '+',
 ('C', '4'): '-',
 ('C', '5'): '+',
 ('C', '6'): '+',
 ('C', '7'): '+',
 ('D', '1'): '+',
 ('D', '2'): '+',
 ('D', '3'): '+',
 ('D', '4'): '-',
 ('D', '5'): '+',
 ('D', '6'): '+',
 ('D', '7'): '+',
 ('E', '1'): '+',
 ('E', '2'): '+',
 ('E', '3'): '+',
 ('E', '4'): '-',
 ('E', '5'): '+',
 ('E', '6'): '+',
 ('E', '7'): '+'}

如果你想坚持改变,我会挑剔dict:

missing = (("B",4),("C",4),("D",4),("E",4))

import os
import pickle
# if file does not exist, this is the first booking
if not os.path.isfile("bookings.pkl"):
    seats = {(x,str(y)):"+" if (x,y) not in missing else "-" for x in ["A","B","C","D","E"] for y in range(1,8)}
else:
    # else use the previous updated dict 
    with open("bookings.pkl") as f:
        seats = pickle.load(f)

while True:
    print("Seats marked + are available\n")
    srt = sorted(seats.iteritems())
    # split into sections based on row and print rows
    sections = [srt[n:n+7] for n in xrange(0,len(srt),7)]
    for sec in sections:
        for seat, status in sec:
             print("{}{}:{status} ".format(*seat,status=status)),
        print("")
    inp = tuple(raw_input("Choose your seat in format row number ie A 1:").upper().split())
    if inp in seats:
        # and not already taken
         if seats[inp] != "*":
            print("You have chosen row: {} seat number: {}\n".format(*inp))
            # ask user to confirm their choice
            confirm = raw_input("Enter y to accept any n to change").lower()
            if confirm == "y":
                seats[inp] = "*"
                with open("bookings.pkl","w") as f:
                    pickle.dump(seats,f)
                    print("Booking confirmed, goodbye.")
                    break
            else:
                 # else let user choose again
                continue
        print("Seat unavailable\n")
    else:
        print("Invalid choice")