2014-12-30 2 views
1

절대 바보 같지만 저는 Python을 처음 사용하고 학교 프로젝트로 영화 예약 시스템을 만들어야합니다.예약 시스템에 CSV 파일을 쓰려면 어떻게해야합니까? [STUCK]

나는 형식의 CSV 파일에 쓸거야 것을 결정했다 :

무료

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

'+'의미 좌석 '-'의미 좌석

존재하지 않습니다

'*'의미 좌석 예약

예를 들어 1 열의 행 A가 무료인지 여부를 확인하고, 그렇다면 * wi를 의미하는지 확인하십시오. 예약하겠습니다 ...

만약 가능하다면 누군가가 올바른 방향으로 나를 가리킬 수 있다면 많은 의무가 있습니다.

나머지 부분에는 아무런 문제가 없습니다. 막 장벽에 부딪 쳤습니다. 내 마음은 연구 시간이 부족하고 인터넷 검색으로는 해결할 수 없습니다. 도움을 많이 주시면 감사하겠습니다.

+1

끔찍한 생각입니다. 10000 석이 있다면 어떨까요? – eliasah

+1

파일에 쓸 필요가없는 경우 'dict' – user2097159

+0

CSV 대신 사용할 수 있습니까? –

답변

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") 
+0

오우! 좋아, 내가 전에 이것을 줘! –

+0

좋아, 이건 내가 확실히이 환호를 사용하는 것입니다. –

+1

@DanielVaughan, 아무런 걱정없이 예약을하고 사전에 변경 사항을 지속하는 방법을 추가 했으므로 좌석 맵을 더 예쁘게 만들 수 있습니다. 그러나 나는 당신에게 맡길 것입니다. –

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

고마워요.이게 내가 필요로하는 전부 야. 여기에서 이해해. 고맙다. –

관련 문제