
예외 처리 키워드
⌨️ a_file.txt가 존재하지 않음
try:
file = open("a_file.txt") # 실제하지 않는 파일을 읽음
a_dictionary = {"key": "value"}
print(a_dictionary["cey"]) # 실제하지 않는 키 입력
except FileNotFoundError:
file = open("a_file.txt", "w")
file.write("something")
except KeyError as error_message:
print(f"The key {error_message} doesn't exist")
else:
content = file.read()
print(content)
finally:
file.close()
print("File was closed")
📄 a_file.txt
something
⌨️ a_file.txt가 존재하는 상태
try:
file = open("a_file.txt")
a_dictionary = {"key": "value"}
print(a_dictionary["cey"]) # 실제하지 않는 키 입력
except FileNotFoundError:
file = open("a_file.txt", "w")
file.write("something")
except KeyError as error_message:
print(f"The key {error_message} doesn't exist")
else:
content = file.read()
print(content)
finally:
file.close()
print("File was closed")
raise errortype("txt")
height = float(input("Height: "))
weight = int(input("Weight: "))
if height > 3:
raise ValueError("Human Height should not be over 3 meters.")
bmi = weight / height ** 2
print(bmi)
IndexError Handling
IndexError를 일으키는 코드 수정하기
- 예외 발생 시 "Fruit pie"만 출력되어야 함
fruits = ["Apple", "Pear", "Orange"]
# 🚨 Do not change the code above
# TODO: Catch the exception and make sure the code runs without crashing.
def make_pie(index):
try:
fruit = fruits[index]
except IndexError:
print("Fruit pie")
else:
print(fruit + " pie")
# 🚨 Do not change the code below
make_pie(4)
KeyError Handling
KeyError를 일으키는 코드 수정하기
facebook_posts = [
{'Likes': 21, 'Comments': 2},
{'Likes': 13, 'Comments': 2, 'Shares': 1},
{'Likes': 33, 'Comments': 8, 'Shares': 3},
{'Comments': 4, 'Shares': 2},
{'Comments': 1, 'Shares': 1},
{'Likes': 19, 'Comments': 3}
]
total_likes = 0
# TODO: Catch the KeyError exception
for post in facebook_posts:
try:
total_likes = total_likes + post['Likes']
except KeyError:
pass
print(total_likes)
🔍 유의 사항
- 유저가 입력한 단어가 딕셔너리에 없다면(숫자, 기호) 다시 입력하도록 피드백하기
⌨️ main.py
import pandas
data = pandas.read_csv("nato_phonetic_alphabet.csv")
# 1. 딕셔너리 생성
phonetic_dict = {row.letter:row.code for (index, row) in data.iterrows()}
# 2. 사용자가 입력한 단어 각각의 문자에 대한 음성규약 단어 리스트 생성
def generate_phonetic():
user_word = input("Enter a word : ").upper()
# Day30에서 수정한 부분(예외 처리)
try:
output_list = [phonetic_dict[letter] for letter in user_word]
except KeyError:
print("Sorry, only letters in the alphabet please.")
generate_phonetic()
else:
print(output_list)
generate_phonetic()
JSON(JavaScript Object Notation)
(python JSON module documentation)
obj : JSON 파일로 내보내려는 것file : 대상 JSON 파일 경로indent : 지정한 정수형 숫자만큼 들여쓰기를 하여 파일을 읽기 쉽게 바꿈separators : 튜플 ( 디폴트 값 = (", ", ": ") )Python objects → JSON 변환
| Python | JSON |
|---|---|
| dict | Object |
| list | Array |
| tuple | Array |
| str | String |
| int | Number |
| float | Number |
| True | ture |
| False | false |
| None | null |
Day29 프로젝트 : 패스워드 매니저 GUI 를 개선한 프로그램
🔍 유의 사항
- Add버튼을 누르면 뜨는 확인창 제거
- txt 파일이 아닌 json 파일에 쓰는 것으로 변경
- 아직은 json 파일이 없는 채로 실행하면 에러가 나는 문제 있음 → 다음 단계
⌨️ main.py
…
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
…
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
# JSON 파일에 저장할 데이터 포멧
new_data = {
website: {
"email": email,
"password": password
}
}
if len(website) == 0 or len(password) == 0:
messagebox.showwarning(message="Please make sure "
"you haven't left any fields empty.")
else:
# JSON
with open("data.json", 'r') as data_file:
data = json.load(data_file)
data.update(new_data)
with open("data.json", 'w') as data_file:
json.dump(data, data_file, indent=4)
website_entry.delete(0, END)
password_entry.delete(0, END)
# ---------------------------- UI SETUP ------------------------------- #
…
📄 data.json
{
"Amazon": {
"email": "example@gmail.com",
"password": "$xvZWwDri4)1+LS"
},
"Ebay": {
"email": "example@gmail.com",
"password": "rz)#20xFSL+ZZg6Z"
}
}
🔍 유의 사항
- 위의 코드를 json 파일이 없는 채로 실행해도 에러가 발생하지 않도록 수정하기
- 파일을 오픈하는 코드가 계속 반복되기 때문에 따로 함수로 만들어도 되지만,
그대로 두면 나중에 코드를 검토할 때 더 이해하기 쉽다는 장점이 있음
⌨️ main.py
…
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
…
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
# JSON 파일에 저장할 데이터 포멧
new_data = {
website: {
"email": email,
"password": password
}
}
if len(website) == 0 or len(password) == 0:
messagebox.showwarning(message="Please make sure "
"you haven't left any fields empty.")
else:
try:
with open("data.json", 'r') as data_file:
data = json.load(data_file)
except FileNotFoundError:
with open("data.json", 'w') as data_file:
json.dump(new_data, data_file, indent=4)
else:
data.update(new_data)
with open("data.json", 'w') as data_file:
json.dump(data, data_file, indent=4)
finally:
website_entry.delete(0, END)
password_entry.delete(0, END)
# ---------------------------- UI SETUP ------------------------------- #
…
🔍 유의 사항
- Search 버튼 추가(웹사이트를 입력 후 누르면 이메일과 비밀번호를 팝업창으로 알려줌)
- 예외 처리
- json 파일이 없는 채로 검색하면 데이터 파일이 없다는 문구를 팝업창으로 띄우기
- json 파일이 있을 때만 저장한 이메일과 패스워들을 팝업창으로 띄우기
- if와 else로 쉽게 예외 처리를 할 수 있는 경우에는 가급적 예외 처리 키워드 사용 지양
⌨️ main.py
…
import json
# ---------------------------- PASSWORD GENERATOR ------------------------------- #
…
# ---------------------------- SAVE PASSWORD ------------------------------- #
…
# ---------------------------- FIND PASSWORD ------------------------------- #
def find_password():
website = website_entry.get()
try:
with open("data.json") as data_file:
data = json.load(data_file)
except FileNotFoundError:
messagebox.showwarning(message="No Data File Found.")
else:
if website in data:
email = data[website]["email"]
password = data[website]["password"]
messagebox.showinfo(title=website,
message=f"Email: {email}\nPassword: {password}")
else:
messagebox.showwarning(message=f"No details for the {website} exists.")
finally:
website_entry.delete(0, END)
# ---------------------------- UI SETUP ------------------------------- #
…
search_button = Button(text="Search", width=13, command=find_password)
search_button.grid(row=1, column=2)
…
파일이 없을 때 검색한 경우
파일에 저장된 웹사이트로 검색한 경우
파일에 저장되지 않은 웹사이트로 검색한 경우