์๋์ ๋ณธ๋ฌธ์ ๋ถ์คํธ์บ ํ AI Tech 1๊ธฐ ์๊ฐ ์ค์ ์์ฑํ ํ์ต ๊ธฐ๋ก์ ๋๋ค๐
1. Exception Handling(์์ธ ์ฒ๋ฆฌ)
1) try ~ except ๊ตฌ๋ฌธ
exceptions์ด ๋ฐ์ํ๋ค๊ณ ํ๋ก๊ทธ๋จ์ด ์ข ๋ฃํ๋ ๊ฒ์ด ์๋๋ค.
๊ทธ ๋ถ๋ถ๋ง ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ณ ๋ค์ ์์ ์ ์ํํ๋ค.
# 0์ผ๋ก ์ซ์๋ฅผ ๋๋ ๋ ์์ธ์ฒ๋ฆฌ ํ๊ธฐ
for i in range(10):
try:
print(10 / i)
except ZeroDivisionError:
print("Not divided by 0")
except ๊ตฌ๋ฌธ์ ๋ ์ถ๊ฐํ ์ ์๋ค.
a = [1, 2, 3, 4, 5]
for i in range(10):
try:
print(i, 10 // i)
except ZeroDivisionError:
print("Not divided by 0")
except IndexError as e:
print(e)
except Exception as e: # ๋ณดํต ๋ง์ง๋ง exception but, ์์ธ๊ฐ ์ด๋์ ๋ฐ์ํ๋ ์ง ๋ค๋ฅธ ์ฌ์ฉ์๊ฐ ์๊ธฐ ์ด๋ฝ๋ค๋ ๋จ์
print(e)
๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ ์์ธ์ธ Built-in-Exception์ ๋ค์๊ณผ ๊ฐ๋ค.
Exception ์ด๋ฆ | ๋ด์ฉ |
IndexError | List์ Index ๋ฒ์๋ฅผ ๋์ด๊ฐ ๋ |
NameError | ์กด์ฌํ์ง ์์ ๋ณ์๋ฅผ ํธ์ถํ ๋ |
ZeroDivisionError | 0์ผ๋ก ์ซ์๋ฅผ ๋๋ ๋ |
ValueError | ๋ณํํ ์ ์๋ ๋ฌธ์/์ซ์๋ฅผ ๋ณํํ ๋ |
FileNotFoundError | ์กด์ฌํ์ง ์๋ ํ์ผ์ ํธ์ถํ ๋ |
2) try ~ except ~ finally ๊ตฌ๋ฌธ
finally ๋ถ๋ถ์ ํญ์ ์คํ๋๋ค.
for i in range(10):
try:
result = 10 // i
except ZeroDivisionError:
print("Not divided by 0")
finally:
print("์ข
๋ฃ๋์์ต๋๋ค.") # ์ด ๋ถ๋ถ์ ์์ธ ๋ฐ์ ์ฌ๋ถ์ ์๊ด์์ด ํญ์ ์คํ๋๋ค.
3) raise ๊ตฌ๋ฌธ
ํ์์ ๋ฐ๋ผ ๊ฐ์ ๋ก Exception์ ๋ฐ์์ํค๊ธฐ ์ํ ๊ตฌ๋ฌธ
while True:
value = input("๋ณํํ ์ ์ ๊ฐ์ ์
๋ ฅํด์ฃผ์ธ์")
for digit in value:
if digit not in "0123456789":
raise ValueError("์ซ์๊ฐ์ ์
๋ ฅํ์ง ์์ผ์
จ์ต๋๋ค.")
print("์ ์๊ฐ์ผ๋ก ๋ณํ๋ ์ซ์ - ", int(value))
4) assert ๊ตฌ๋ฌธ
ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์์ ๊ฒฝ์ฐ ์์ธ๋ฅผ ๋ฐ์์ํจ๋ค.
# ์ฌ์ฉ์๊ฐ decimal๋ก ์
๋ ฅํด์ผ๋ง ํ๋ ์กฐ๊ฑด
def get_binary_number(decimal_number : int):
assert isinstance(decimal_number, int) # assert ๋ท ๋ถ๋ถ์ True/False๊ฐ ํ๋ณ๋์ด์ผ ํ๋ค.
return bin(decimal_number)
print(get_binary_number(10.0)) # AssertionError ๋ฐ์
2. File Handling
1) File Read
- open, close ๋ฉ์๋
f = open("text.txt", "r") # ๋์ ํ์ผ์ด ๊ฐ์ ํด๋์ ์์ด์ ์๋ ์ฃผ์๋ก ์ ๊ทผ
contents = f.read()
print(contents)
f.close()
- with ๊ตฌ๋ฌธ
indentation์ด ๋๋๋ ๋ถ๋ถ์์ ์๋์ผ๋ก file close๊ฐ ์ผ์ด๋๋ค.
with open("text.txt", "r") as my_file:
contents = my_file.read()
print(type(contents), contents)
๊ทผ๋ฐ, contents์ my_file ๊ฐ์ด with ๊ตฌ๋ฌธ์ ๋ฒ์ด๋๋ฉด ์ฝํ๊น? python์ ๋ณ์ ์ค์ฝํ
- readlines() ๊ตฌ๋ฌธ
ํ ์ค์ฉ ์ฝ์ด ๋ฉ๋ชจ๋ฆฌ์ ๋ฆฌ์คํธ ํํ๋ก ํ ๋ฒ์ ์ฌ๋ฆฐ๋ค.
with open("text.txt", "r") as f:
ctnt_list = f.readlines()
print(ctnt_list) # ๋ฆฌ์คํธ ๊ฐ ์ถ๋ ฅ
- readline() ๊ตฌ๋ฌธ
์คํ ์๋ง๋ค ํ ์ค์ฉ ์ฝ์ด์จ๋ค.
with open("text.txt", "r") as f:
i = 0
while True:
line = f.readline()
if not line:
break
print(str(i) + " === " + line.replace("\n", ""))
i = i + 1
2) File Write
mode๋ "w", encoding="utf8"
f = open("write.txt", "w", encoding="utf8")
for i in range(1, 11):
data = f"{i}๋ฒ์งธ ์ค์
๋๋ค.\n"
f.write(data)
f.close()
"a" ๋ชจ๋๋ append๋ก ๊ธฐ์กด ๋ด์ฉ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ด๋ค.
with open("write.txt", "a", encoding="utf8") as f:
for i in range(11, 21):
data = f"{i}๋ฒ์งธ ์ค์
๋๋ค.\n"
f.write(data)
3) Directory ๋ค๋ฃจ๊ธฐ
- os ๋ชจ๋์ ์ฌ์ฉํ์ฌ directory๋ฅผ ๋ค๋ฃฐ ์ ์๋ค.
import os
try:
os.mkdir("abc")
except FileExistsError as e:
print(e) # ์ด๋ฏธ ์กด์ฌํ๋ ๊ฒฝ๋ก๋ผ๋ฉด [Errno 17] File exists: 'abc' ์ถ๋ ฅ
์ด๋ ๊ฒ ์ฒดํฌํ๊ธฐ๋ ํ๋ค.
os.path.exists("abc")
- pathlib ๋ชจ๋์ ์ฌ์ฉํ์ฌ path๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ๋ค๋ฃธ
import pathlib
cwd = pathlib.Path.cwd()
print(cwd) # /Users/username/Downloads/Coding/01 - Portfolio/06 - Boostcamp AI Tech/U stage/week01
print(cwd.parent) # /Users/username/Downloads/Coding/01 - Portfolio/06 - Boostcamp AI Tech/U stage
print(cwd.parent.parent) # /Users/username/Downloads/Coding/01 - Portfolio/06 - Boostcamp AI Tech
directory๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ๋ค๋ฃจ๋ฉด์ ์ ๊ทผ์ ํจ์ฌ ์ฉ์ดํ๋ค!!
4) Pickle(๊ฐ์ฒด๋ฅผ persistent ํ๊ฒ ๋ง๋๋ Python built-in ๋ชจ๋)
- ๋ฐ์ดํฐ, object ๋ฑ ์คํ ์ค์ธ ์ ๋ณด๋ฅผ ์ ์ฅ/๋ถ๋ฌ์์ ์ฌ์ฉํ๋ค.
- ์ ์ฅํด์ผ ํ๋ ์ ๋ณด, ๊ณ์ฐ ๊ฒฐ๊ณผ(๋ชจ๋ธ) ๋ฑ ํ์ฉ์ด ๋ค์ํ๋ค.
- binary ํ์ผ๋ก ์ด๋ ค ์ธ๋ถ์์ ์ฝ์ ์๋ ์๋ค.
import pickle
f = open("list.pickle", "wb")
test = [1, 2, 3, 4, 5]
pickle.dump(test, f) # test ๊ฐ์ฒด๋ฅผ list.pickle์ ๋ฐ์ด๋๋ฆฌ ํ์ผ๋ก ์ ์ฅ
f.close()
f = open("list.pickle", "rb")
test_pickle = pickle.load(f)
print(test_pickle) # [1, 2, 3, 4, 5]
f.close()
3. Data Handling
1) CSV(Comma Seperate Values)
- ํ๋๋ฅผ ์ผํ(,)๋ก ๊ตฌ๋ถํ ํ ์คํธ ํ์ผ
- ํญ(TSV), ๋น์นธ(SSV), ๋ฑ์ผ๋ก ๊ตฌ๋ถํด์ ๋ง๋ค๊ธฐ๋ ํ๋ค.
- ํต์นญํ์ฌ Character-Seperated-Values(CSV)๋ผ๊ณ ๋ถ๋ฅธ๋ค.
์ผ๋ฐ ๋ฌธ์์ด ์ฒ๋ฆฌํ๋ฏ ํ ์๋ ์์ง๋ง, Python์์ CSV ๊ฐ์ฒด๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค.
import csv
reader = csv.reader(f,
delimiter=',', quotechar='"',
quoting=csv.QUOTE_ALL)
Attribute | Default | Meaning |
delimiter | , | ๊ธ์๋ฅผ ๋๋๋ ๊ธฐ์ค |
lineterminator | \r\n | ์ค ๋ฐ๊ฟ ๊ธฐ์ค |
quotechar | " | ๋ฌธ์์ด์ ๋๋ฌ์ธ๋ ์ ํธ ๋ฌธ์ "์ฌ๊ธฐ๋ถํฐ ์ฌ๊ธฐ๊น์ง๋ฅผ ํ๋์ ํ๋๋ก ๋ณด์ธ์" |
quoting | QUOTE_MINIMAL | ๋ฐ์ดํฐ ๋๋๋ ๊ธฐ์ค์ด quotechar์ ์ํด ๋๋ฌ์ธ์ธ ๋ ๋ฒจ |
*์ถ๊ฐ
์ด์ ํผ์ด ์ธ์ ์ ํ์๋ถ๋ค๊ณผ ์ ๋ ๊ณต๋ถํ ๋ด์ฉ์ ๋๋๊ธฐ๋ก ํ๋ค.
๋ชจ์ด๋ ์๊ฐ๋๊ฐ ์ ๋งคํด์ ๋ค๋ค ํ์ฐธ ์์ ์ ๋ฃ๊ณ ๊ณต๋ถ๋ฅผ ํ๋ ์ค๊ฐ์ ๋ชจ์๋ค.
๊ทธ๋์ ๋ง์ ์๊ธฐ๋ฅผ ๋ชปํด๋ณธ๋ค๋ ๋ฌธ์ ๊ฐ ์์๊ธฐ ๋๋ฌธ์ด๋ค.
์ค๋ ์ฒ์ ์ ๋ ๊ณต๋ถํ ๋ด์ฉ์ ์ด์ผ๊ธฐํ๊ณ ํ์๋ผ๋ฆฌ ๊ณผ์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํ๋๋ฐ ์ ์ผ ์ฌ๋ฐ์๋ค.๐
๋๋ณด๋ค ๋ ๊ฐ๋ ์ฑ ์ข๊ณ ํจ์จ์ ์ผ๋ก ์ง ์ฝ๋๋ ์์๊ณ
๋ค๋ฅธ ์๊ฐ์ผ๋ก ์ ๊ทผ์ ํด๋ณธ ์ฝ๋๋ฅผ ๋ณธ๋ค๋ ๊ฒ ์ข์๋ค!
๋ ๋ถํธ์บ ํ๋ฅผ ํ๊ณ ์ถ์๋ ํฐ ์ด์ ์ค์ ํ๋์ธ ์ฝ๋ ๋ฆฌ๋ทฐ ๊ธฐํ๊ฐ ๋๋ฌด ์ข๋ค!!
'Languages > Python ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] python์ ๋ณ์ ์ค์ฝํ (0) | 2021.12.02 |
---|---|
[Python] Python์ ์๋ฃ๊ตฌ์กฐ (0) | 2021.01.21 |
[Python] Immutable ๊ฐ์ฒด์ Mutable ๊ฐ์ฒด (0) | 2021.01.19 |