표준 입출력
print('aaa', 'bbb', 'ccc', sep = '+')
print('aaa', 'bbb', 'ccc', sep = '')
print('aaa', 'bbb', 'ccc', sep = ' ')
print('aaa', 'bbb', 'ccc', sep = ',')
print('aaa', 'bbb', 'ccc', sep = 'zzz')
print('aaa', 'bbb', 'ccc', sep = '\n')
print('aaa', 'bbb', 'ccc', sep = ' ', end = '?')
print('ddd')
print('zzz')
import sys
print('aaa', 'bbb', file = sys.stdout)
print('aaa', 'ccc', file = sys.stderr)
dic = {'aaa' : 1111111, 'bbbbb' : 2222, 'cccccccc' : 33}
for abc, num in dic.items():
print(abc.ljust(10), str(num).rjust(10), sep = ':')
for num in range(1, 21):
print(str(num).zfill(3))
answer = input('enter : ')
print("answer :", answer)
다양한 출력 포맷
print("{0: >10}".format(500))
print("{0: >+10}".format(500))
print("{0: >+10}".format(-500))
print("{0:_<10}".format(500))
print("{0:,}".format(1000000000000))
print("{0:^<+30,}".format(100000000000))
print("{0:f}".format(5/3))
print("{0:.2f}".format(5/3))
파일 입출력
score_file = open("score.txt", "w", encoding = "utf8")
print("a : 0", file = score_file)
print("b : 50", file = score_file)
score_file.close()
score_file = open("score.txt", "a", encoding = "utf8")
score_file.write('c : 100')
score_file.write('\nd : 150')
score_file.close
score_file = open('score.txt', 'r', encoding = 'utf8')
print(score_file.read())
score_file.close()
score_file = open('score.txt', 'r', encoding = 'utf8')
print(score_file.readline(), end = '')
print(score_file.readline(), end = '')
print(score_file.readline(), end = '')
print(score_file.readline())
score_file.close()
score_file = open('score.txt', 'r', encoding = 'utf8')
while True:
line = score_file.readline()
if not line:
break
print(line, end = '')
print("\n")
score_file.close()
score_file = open('score.txt', 'r', encoding = 'utf8')
lines = score_file.readlines()
for line in lines:
print(line, end = '')
score_file.close()
Pickle
import pickle
profile_file = open('profile.pickle', 'wb')
profile = {'name' : 'abc', 'age' : '20', 'hobby' : ['a, b, c']}
print(profile)
pickle.dump(profile, profile_file)
profile_file.close()
profile_file = open('profile.pickle', 'rb')
profile = pickle.load(profile_file)
print(profile)
profile_file.close()
With
import pickle
with open('profile.pickle', 'rb') as profile_file:
print(pickle.load(profile_file))
> with를 사용하면 파일을 닫을 필요 없이 자동으로 종료
with open('study.txt', 'w', encoding = 'utf8') as study_file:
study_file.write('abc123')
with open('study.txt', 'r', encoding = 'utf8') as study_file:
print(study_file.read())
Quiz 풀이
for i in range(1, 51):
with open(str(i) + '주차.txt', 'w', encoding = 'utf8') as r_file:
r_file.write('- {0}주차 주간보고 -'.format(i))
r_file.write('\n부서 : ')
r_file.write('\n이름 : ')
r_file.write('\n업무 요약 : ')