2017-12-11 3 views
-1

피클과 함께 파이썬을 통해 레지스트라 시스템을 만들려고합니다. 사용자 입력을 기록하는 시스템을 확보했지만 향후 프로그램 구현을 위해 저장하지 않습니다. 여기 파이썬 피클이 데이터를 저장하지 않습니다

프로그램이 시작됩니다 코드입니다 :

import datetime 
import pandas as pd 
import pickle as pck 
import pathlib 
from pathlib import * 
from registrar import * 

prompt = "Please select an option: \n 1 Create a new course \n 2 Schedule a new course offering \n 3 List this school's course catalogue \n 4 List this school's course schedule \n 5 Hire an instructor \n 6 Assign an instructor to a course \n 7 Enroll a student \n 8 Register a student for a course \n 9 List this school's enrolled students \n 10 List the students that are registered for a course \n 11 Submit a student's grade \n 12 Get student records \n 13 Exit" 

farewell = "Thank you for using the Universal University Registrar System. Goodbye!" 

print ("Welcome to the Universal University Registration System.") 

print ("\n") 

try: #As long as CTRL-C has not been pressed, or 13 not been input by user. 

    input_invalid = True 
    while input_invalid: 
    inst = input("Please enter the name of your institution. ").strip() 
    domain = input("Please enter the domain. ").strip().lower() 
    if inst == "" or domain == "": 
     print("Your entry is invalid. Try again.") 
    else: 
     input_invalid = False 

    schoolie = Institution(inst, domain) 

    if Path(inst + '.pkl').exists() == False: 
    with open(inst + '.pkl', 'r+b') as iptschool: 
     schoolie = pck.load(iptschool) 

    while True: 
    print (prompt) 
    user_input = input("Please enter your choice: ") 
    try: 
     user_input = int(user_input) 
     if user_input < 1 or user_input > 14: #UserInput 14: on prompt. 
     raise ValueError("Please enter a number between 1 and 13, as indicated in the menu.") 
    except ValueError: 
     print("Not a valid number. Please try again.") 

    if user_input == 1: #Create a new course 
     input_invalid2 = True #Ensure that the user actually provides the input. 
     while input_invalid2: 
     input_name = input("Please enter a course name: ").strip() 
     input_department = input("Please enter the course's department: ").strip() 
     input_number = input("Please enter the course's number (just the number, not the departmental prefix): ").strip() 
     try: 
      input_number = int(input_number) 
     except ValueError: 
      print ("Please print an integer. Try again.") 
     input_credits = input("Please enter the number of credits awarded for passing this course. Please use an integer: ").strip() 
     try: 
      input_credits = int(input_credits) 
     except ValueError: 
      print ("Please print an integer. Try again.") 

     if input_name != "" and input_department != "" and input_number and input_credits: 
      input_invalid2 = False #Valid input 
     else: 
      print("One or more of your entries is invalid. Try again.") 

     added_course = Course(input_name, input_department, input_number, input_credits) 
     for course in schoolie.course_catalog: 
     if course.department == input_department and course.number == input_number and course.name == input_name: 
      print("That course is already in the system. Try again.") 
      input_invalid2 == True 
     if input_invalid2 == False: 
     schoolie.add_course(added_course) 
     print ("You have added course %s %s: %s, worth %d credits."%(input_department,input_number,input_name, input_credits)) 

를 그리고 여기가 저장되는 공개해야하는 두 번째 옵션은,하지만, 그렇지 않습니다.

elif user_input == 2: #Schedule a course offering 
     input_invalid2 = True #Ensure that the user actually provides the input. 
     while input_invalid2: 
     input_department = input("Please input the course's department: ").strip() 
     input_number = input("Please input the course's number: ").strip() 
     course = None 
     courseFound = False 
     for c in schoolie.course_catalog: 
      if c.department == input_department and c.number == input_number: #Course found in records 
      courseFound = True 
      course = c 
      input_section_number = input("Please enter a section number for this course offering: ").strip() 
      input_instructor = input("If you would like, please enter an instructor for this course offering: ").strip() 
      input_year = input("Please enter a year for this course offering: ").strip() 
      input_quarter = input("Please enter the quarter in which this course offering will be held - either SPRING, SUMMER, FALL, or WINTER: ").strip().upper() 
      if input_course != "" and input_course in schoolie.course_catalog and input_section_number.isdigit() and input_year.isdigit() and input_quarter in ['SPRING', 'SUMMER', 'FALL', 'WINTER'] and input_credits.isdigit(): 
       if input_instructor != "": #Instructor to be added later, if user chooses option 6. 
       added_course_offering = CourseOffering(c, input_section_number, None, input_year, input_quarter) 
       else: 
       added_course_offering = CourseOffering(c, input_section_number, input_instructor, input_year, input_quarter) 
       schoolie.add_course_offering(added_course_offering) 
       input_invalid2 = False #Valid input 
       print ("You have added course %s, Section %d: %s, worth %d credits."%(input_course,input_section_number,input_name, input_credits)) 
      else: 
       print("One or more of your entries is invalid. Try again.") 
     if courseFound == False: #If course has not been found at the end of the loop: 
      print("The course is not in our system. Please create it before you add an offering.") 
      break 

그런데 시스템을 제대로 닫은 것 같습니다. 내가 틀렸다면 나를 바로 잡아라.

elif user_input == 13: #Exit 
     with open(inst + '.pkl', 'wb') as output: 
     pck.dump(schoolie, output, pck.HIGHEST_PROTOCOL) 
     del schoolie 
     print (farewell) 
     sys.exit() 

except KeyboardInterrupt: #user pushes Ctrl-C to end the program 
    print(farewell) 

나는 피클 파일을 설정하는 방법에 문제가 있다고 생각한다. 나는 그것들을 만들고 있지만 그것들에 자료를 넣지 않는 것 같다.

이 질문에 대한 사과는 오래 가지 만, 자세한 내용을 통해 내가 겪고있는 문제를 이해하는 데 도움이되기를 바랍니다. 도움에 미리 감사드립니다!

+0

? 피클 파일에 데이터를 저장하는 것은 올바른 방법이 아닙니다. 대신 CSV 파일 또는 SQLite를 사용할 수 있습니다. –

+1

대부분의 코드는'pickle' 문제와 관련이 없습니다 (그리고 모든 긴 줄로 읽는 것은 어렵습니다). 코드가 산세 문제에만 초점을 둔 [mcve] 인 경우 더 좋을 것입니다. 그러나 나는 이상한 일들을 발견했다. 예를 들어, .pkl 파일이'if Path (inst + '.pkl'). exists() == False :'와 함께 존재하지 않으면 테스트하지만 어쨌든 그것을 읽으려고합니다! –

+0

관찰에 감사드립니다. 나는 파일이 존재하지 않는다면 프로그램이 그것을 작성해야한다고 쓰려고했다. 선이 없습니다. –

답변

0

당신이 덤프 및로드는 반전 할 수 있습니다 같다 : (워드 프로세서)

Signature: pck.load(file, *, fix_imports=True, encoding='ASCII', errors='strict') 
Docstring: 
Read and return an object from the pickle data stored in a file. 

Signature: pck.dump(obj, file, protocol=None, *, fix_imports=True) 
Docstring: 
Write a pickled representation of obj to the open file object file. 
정확히 당신이 뭘 하려는지
관련 문제