Coding/Python

[python] ์ž…์ถœ๋ ฅ(ํ‘œ์ค€์ž…์ถœ๋ ฅ, ๋‹ค์–‘ํ•œ ์ถœ๋ ฅํฌ๋งท, ํŒŒ์ผ์ž…์ถœ๋ ฅ, pickle, with)

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 10. 24. 23:19

ํ‘œ์ค€ ์ž…์ถœ๋ ฅ

#sep, end

print("python","java", sep=" vs ") #sep๊ฐ’์„ ์„ค์ •ํ•ด์„œ ๊ฐ , ์‚ฌ์ด์— ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ
print("python","java", sep=" , " , end = "?") #end์˜ ์˜๋ฏธ๋Š” ๋ฌธ์žฅ์˜ ๋งˆ์ง€๋ง‰์„ ?๋กœ ํ•ด๋‹ฌ๋ผ. 

 

#์ขŒ์šฐ์ •๋ ฌ(rjust(), ljust())  
scores = {"์ˆ˜ํ•™":0, "์˜์–ด":50, "์ฝ”๋”ฉ":100}
for subject, score in scores.items():
print(subject, score)
print(subject.ljust(8), str(score).rjust(4), sep=":") #๊ณผ๋ชฉ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜๋Š”๋ฐ, 8์นธ ๋‚จ๊ธฐ๊ณ  ์ •๋ ฌํ•ด๋‹ฌ๋ผ / score ๋ถ€๋ถ„์€ ์˜ค๋ฅธ์ชฝ ์ •๋ ฌํ•˜๋Š”๋ฐ 4์นธ ๊ณต๊ฐ„ ํ™•๋ณด

 

#1์„ 001๋กœ ๋งŒ๋“ค๊ธฐ(zfill())

#์€ํ–‰ ๋Œ€๊ธฐ ์ˆœ๋ฒˆํ‘œ
#001, 002, 003 ...
for num in range(1, 21):
print("๋Œ€๊ธฐ๋ฒˆํ˜ธ : "+ str(num).zfill(3))

 

#ํ‘œ์ค€ ์ž…๋ ฅ
answer = input("์•„๋ฌด ๊ฐ’์ด๋‚˜ ์ž…๋ ฅํ•˜์„ธ์š” : ") #input ๊ฐ’์€ str์œผ๋กœ ๋ฐ›๋Š”๋‹ค. 
print("์ž…๋ ฅํ•˜์‹  ๊ฐ’์€ " + answer +"์ž…๋‹ˆ๋‹ค.")

 

 

๋‹ค์–‘ํ•œ ์ถœ๋ ฅ ํฌ๋งท

#> ์˜ค๋ฅธ์ชฝ์ •๋ ฌ / <์™ผ์ชฝ์ •๋ ฌ / ๋งˆ์ง€๋ง‰ ,๋Š” ์„ธ์ž๋ฆฌ๋งˆ๋‹ค ์ฐ๋Š” ,

# ๋นˆ ์ž๋ฆฌ๋Š” ๋นˆ๊ณต๊ฐ„์œผ๋กœ ๋‘๊ณ , ์˜ค๋ฅด์ชฝ ์ •๋ ฌํ•˜๋˜, ์ด 10์ž๋ฆฌ ๊ณต๊ฐ„ ํ™•๋ณด
print("{0: >10}".format(500))

#์–‘์ˆ˜์ผ ๋• +๋กœ ํ‘œ์‹œ, ์Œ์ˆ˜์ผ ๋• -๋กœ ํ‘œ์‹œ
print("{0: >+10}".format(500))
print("{0: >+10}".format(-500))

#์™ผ์ชฝ ์ •๋ ฌํ•˜๊ณ , ๋นˆ์นธ์œผ๋กœ _๋กœ ์ฑ„์›€
print("{0:_<+10}".format(500))

#์„ธ ์ž๋ฆฌ๋งˆ๋‹ค ,๋ฅผ ์ฐ์–ด์ฃผ๊ธฐ
print("{0:,}".format(10000000000))

#์„ธ ์ž๋ฆฌ๋งˆ๋‹ค ,๋ฅผ ์ฐ์–ด์ฃผ๊ธฐ, +-๋„ ๋ถ™์ด๊ธฐ
print("{0:+,}".format(10000000000))

#์„ธ ์ž๋ฆฌ๋งˆ๋‹ค ,๋ฅผ ์ฐ์–ด์ฃผ๊ธฐ, +-๋„ ๋ถ™์ด๊ธฐ, ์ž๋ฆฟ์ˆ˜ ํ™•๋ณดํ•˜๊ธฐ, ๋นˆ์ž๋ฆฌ๋Š” ^๋กœ ์ฑ„์›Œ์ฃผ๊ธฐ
print("{0:^<+30,}".format(10000000000))


#์†Œ์ˆ˜์  ์ถœ๋ ฅ
print("{0:f}".format(5/3))

#์†Œ์ˆ˜์  ํŠน์ • ์ž๋ฆฌ์ˆ˜๊นŒ์ง€ ์ถœ๋ ฅ(์†Œ์ˆ˜์  ์…‹์จฐ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผ)
print("{0:.2f}".format(5/3))

 

ํŒŒ์ผ ์ž…์ถœ๋ ฅ

#ํŒŒ์ผ ์“ฐ๊ธฐ "w"

score_file = open("score.txt", "w", encoding="utf8") #์“ฐ๊ธฐ ์šฉ๋„
print("์ˆ˜ํ•™ : 0", file=score_file)
print("์˜์—ฌ : 50", file=score_file)
score_file.close()  #์—ฐ ํŒŒ์ผ์€ ํ•ญ์ƒ ๊ผญ ๋‹ซ์•„์ฃผ์–ด์•ผ ํ•ด์š”! 

 

#๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ "a"
score_file = open("score.txt", "a", encoding="utf8") #append
score_file.write("๊ณผํ•™ : 80")
score_file.write("\n์ฝ”๋”ฉ : 100")
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()) #์ค„๋ณ„๋กœ ์ฝ๊ธฐ, ํ•œ ์ค„ ์ฝ๊ณ  ์ปค์„œ๋Š” ์•„๋ž˜๋กœ ์ด๋™
print(score_file.readline())
print(score_file.readline())
print(score_file.readline())


#ํŒŒ์ผ ๋ช‡ ์ค„์ธ์ง€ ๋ชจ๋ฅผ ๋•Œ
score_file = open("score.txt", "r", encoding="utf8")
while True :
line = score_file.readline()
if not line :
break
print(line, end="")
score_file.close()


#Listํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์—ฌ ์ฝ๊ธฐ
score_file = open("score.txt", "r", encoding="utf8")
lines = score_file.readlines() #list ํ˜•ํƒœ๋กœ ์ €์žฅ
for line in lines:
print(line, end="")
score_file.close()

picke

#๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ pickle์„ ์ด์šฉํ•ด์„œ ์ €์žฅํ•˜๊ณ , load๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์œ ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

import pickle
profile_file = open("profile.pickle","wb") #b๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์˜๋ฏธ, ํ”ผํด์„ ์“ฐ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ญ์ƒ ๋ฐ”์ด๋„ˆ๋ฆฌ ์ •์˜ ํ•„์š”
profile = {"์ด๋ฆ„": "๋ฐ•๋ช…์ˆ˜", "๋‚˜์ด": 30, "์ทจ๋ฏธ":["์ถ•๊ตฌ", "์•ผ๊ตฌ", "์ฝ”๋”ฉ"]}
print(profile)
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()

 

 

with

#with๋Š” ํŒŒ์ผ์„ close ํ•  ํ•„์š”๊ฐ€ ์—†์Œ
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'์™€ ๊ฐ™์ด ๋งŒ๋“ญ๋‹ˆ๋‹ค.

ํ’€์ด: 

๋”๋ณด๊ธฐ
weeknum = 1
for weeknum in range (1, 51):
    with open("{0}์ฃผ์ฐจ.txt".format(weeknum), "w", encoding="utf8") as reporting_file:
        reporting_file.write(
            """- {0}์ฃผ์ฐจ ์ฃผ๊ฐ„๋ณด๊ณ   -\n๋ถ€์„œ:\n์ด๋ฆ„: \n์—…๋ฌด ์š”์•ฝ""".format(weeknum)
        )

 

ํ•ด์„ค:

๋”๋ณด๊ธฐ
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์—…๋ฌด ์š”์•ฝ:")