[파이썬] 기초 공부, 표준입출력, 다양한 출력포맷, pickle, with

 

 표준입출력, 다양한 출력포맷, pickle, with

개발하시는 분들은 알고리즘의 최강자가 아니실까 싶다.

1. 표준입출력

print("pyton""java")
print("pyton""java"sep=",")
print("pyton""java""javaScript"sep=" vs ")
print("pyton""java"sep=",")
print("무엇이 더 재미있을까요?")
print("pyton""java"sep=","end="?")
print("무엇이 더 재미있을까요?")







import sys
print("pyton""java"file=sys.stdout#표준출력
print("pyton""java"file=sys.stderr#표준에러처리 별도 로깅
 




scores = {"수학":0"영어":50"코딩":100 }  #딕셔너리 선언
for subjectscore in scores.items():   #subject, score 변수선언후 
    print(subjectscore)
    
scores = {"수학":0"영어":50"코딩":100 }
for subjectscore in scores.items():
    print(subject.ljust(4), str(score).rjust(4), sep=":")  #정렬










for num in range(121): #1부터 20까지의 수
    print("대기번호 : " + str(num).zfill(3))
#zfill, 3자리만큼 표시하고, 값이 비는 자리는 0으로 채운다. 

    














answer = input("아무값이 입력하세요 : ")  #사용입력을 통해 값을 받을 경우는 항상 str(문자형태)로 인식한다. 
print("입력하신 값은 " + answer + "입니다.")
print(type(answer))







2. 다양한 출렷 포맷


#빈자리는 빈공간으로 두고, 오른쪽 정렬을 하되, 총 10자리 공간을 확보
print("{0: >10}" .format(500))
print("{0: >10}" .format(-500))   
#양수일때는 + 표시, 음수일 때는 -로 표시
print("{0: >+10}" .format(500))
print("{0: >+10}" .format(-500))
#왼쪽정렬하고, 빈칸을 _로 채움
print("{0:_<10}" .format(500))
print("{0:_<+10}" .format(500))
#3자리마다 콤마를 찍어주기
print("{0:,}" .format(1000000000))
#3자리마다 콤마를 찍어주기, + - 부호 붙이기
print("{0:+,}" .format(1000000000))
print("{0:+,}" .format(-1000000000))
#3자리마다 콤마를 찍어주기, 부호도 붙이고, 자릿수 확보하기 / 빈자리는 ^로 채우기 
print("{0:^<+30,}" .format(1000000000))
#소수점 출력
print("{0:f}" .format(5/3))
#소수점을 특정자리수까지 (소수점 3째자리에서 반올림)
print("{0:.2f}" .format(5/3))










3. 파일 입출력


#파일 입출력
score_file = open("scroe.txt""w"encoding="utf8"# w= 쓰기목적
print("수학 : 0"file=score_file)
print("영어 : 0"file=score_file)
score_file.close()











score_file = open("scroe.txt""a"encoding="utf8"# a= 추가로 작성
score_file.write("과학 : 80")
score_file.write("\n코딩 : 100")
score_file.close()











score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
print(score_file.read())
score_file.close()







score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
print(score_file.readline()) #라인별로 읽고, 한줄 읽고 커서는 다음줄 로 이동
print(score_file.readline())
print(score_file.readline())
print(score_file.readline())
score_file.close()










score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
print(score_file.readline(), end=""#라인별로 읽고, 한줄 읽고 커서는 다음줄 로 이동 , 줄간격 없애기
print(score_file.readline(), end="")
print(score_file.readline(), end="")
print(score_file.readline(), end="")
score_file.close()







score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
while True:
    line = score_file.readline()
    if not line:
        break
    print(line)
score_file.close()


score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
while True:
    line = score_file.readline()
    if not line:
        break
    print(lineend="")
score_file.close()










score_file = open("scroe.txt""r"encoding="utf8"# r=파일 읽어오기
lines = score_file.readlines() #list 형태로 저장
for line in lines:
    print(lineend="")

score_file.close()






4. pickle


profile_file = open("profile.pickle""wb")
profile = {"이름":"박명수""나이":30"취미":["축구","골프","코딩"]}
print(profile)
pickle.dump(profileprofile_file)  #profile의 정보를 file저장
profile_file.close()













profile_file = open("profile.pickle""rb"
profile = pickle.load(profile_file#file에 있는 정보를 Profile에 불러오기
print(profile)
profile_file.close()




5. with


#with 작업
import pickle

with open("profile.pickle""rb"as profile_file:   #파일을 profile_file 변수에 저장 
    print(pickle.load(profile_file))






with open("study.txt""w"encoding="utf8"as study_file:
    study_file.write("파이썬 공부")

 








with open("study.txt""r"encoding="utf8"as study_file:
    print(study_file.read())


6. 퀴즈 

---------------------------  내가 풀은것 


for report_week in range (1,11): #1부터 10주차까지
    report_file = openstr(report_week) + "주차.txt""w"encoding="utf8")    
    print("- " + str(report_week) + " 주차 보고서 -"file=report_file)
    print("부서 :"file=report_file)
    print("이름 :"file=report_file)
    print("업무 요약 :"file=report_file)
    report_file.close()

-------------------------- 영상 답


for i in range(11,16):
        with open(str(i) + "주차.txt""w"encoding="utf8"as report_file:
            report_file.write("- {0} 주차 주간보고 -" .format(i))
            report_file.write("\n부서 : ")
            report_file.write("\n이름 : ")
            report_file.write("\n업무 요약: ")


결과는 같지만, 코드를 얼마나 간단하고 쉽게 작성하느냐가 중요한 것 같음.


댓글 쓰기

0 댓글