파이썬 공부 5

ysk1230·2022년 9월 12일
0

PYTHON

목록 보기
6/7

#8-1~#8-6

1. 표준입출력

print("python", "java", sep=" , ", end="?") # 중간에 구분값을 넣는다.
print("무엇이 더 재밌을까요?") # 위에 end ='?'를 적으면 줄바꿈하던 기본값을 물음표로 대체해라는 의미로 바뀜
import sys
print("python", "java", file=sys.stdout)
print("python", "java", file=sys.stderr)

시험성적 보여주기

scores = {"수학" : 0, "영어" : 50, "코딩":100}
for subject, score in scores.items():
     print(subject.ljust(8), str(score).rjust(4), sep=":") #왼쪽 정렬, 오른쪽 정렬, 중간은 : 로 처리

은행 대기순번표

예시) 001 , 002 , 003, ....

 for num in range(1,21):
     print("대기번호 :" + str(num).zfill(3))

사용자 입력값

answer = input("아무값이나 입력하세요 : ")
print(type(answer))
print("입력하신 값은 " + answer + "입니다.") # 사용자한테 입력받는 값은 무조건 str이다.

2. 다양한 출력포멧

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)) # 소수점 특정 자리수 까지만 표시
print("{0:.2f}".format(5/3)) # 소수점 3째 자리에서 반올림

3. 파일입출력

score_file = open("score.txt", "w", encoding = "utf8") # w는 쓰기용도
print("수학 : 0", file = score_file)
print("영어 : 50", file = score_file)
score_file.close()
score_file = open("score.txt", "a", encoding = "utf8") # a는 추가 용도
score_file.write("과학 : 80")
score_file.write("\n코딩 : 100")
score_file.close()
score_file = open("score.txt", "r", encoding = "utf8") # r는 읽기용도
print(score_file.read())
score_file.close()
score_file = open("score.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("score.txt", "r", encoding = "utf8") # r는 읽기용도 , 파일이 몇줄인지 모를때 반복문을 통해 쭉 가져오기 가능
while True:
     line = score_file.readline()
     if not line:
         break
     print(line)
 score_file.close()
score_file = open("score.txt", "r", encoding = "utf8") #
lines = score_file.readlines() # list 형태에 담아서 보여주는 것도 가능
for line in lines:
     print(line, end= "")
score_file.close()

4. pickle

import pickle
profile_file = open("profile.pickle", "wb")
profile = {"이름":"박명수", "나이": 30, "취미":["축구","골프","코딩"]}
print(profile_file)
pickle.dump(profile,profile_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

import pickle
with open("profile.pickle", "rb") as profile_file: # as 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())

퀴즈

당신 회사에서는 매주 1회 작성해야 하는 보고서가 있습니다.
보고서는 항상 아래와 같은 형태로 출력되어야 합니다.

  • X주차 주간보고 -
    부서 :
    이름 :
    업무 요약 :

  • 1주차~50주차 까지의 보고서 파일을 만드는 프로그램을 작성하시오.
    조건 : 파일명은 '1주차.txt', '2주차.txt', ... 와 같이 만듭니다.

for i in range(1,51):
     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개의 댓글

관련 채용 정보