๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT-Engineering/Python

Python format ํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ ์•ˆ์— ๊ด„ํ˜ธ ๋„ฃ๊ธฐ - ํŒŒ์ด์ฌ ๊ฟ€ํŒ

by ๐Ÿงž‍โ™‚๏ธ 2020. 7. 27.
๋ฐ˜์‘ํ˜•

Python formatํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ต‰์žฅํžˆ ํŽธํ•˜๊ฒŒ ๋‹ค์–‘ํ•œ ๋ณ€์ˆ˜๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์˜ ์˜ˆ์ฒ˜๋Ÿผ { } ์ค‘๊ด„ํ˜ธ ํ‘œ์‹œ์™€ ๊ด„ํ˜ธ ์•ˆ์— :์ „์ฒด์ž๋ฆฌ์ˆ˜.์†Œ์ˆ˜์ ์ž๋ฆฌ์ˆ˜+์˜ต์…˜ ํ˜•ํƒœ๋กœ ํŽธํ•˜๊ฒŒ string์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

homepath = '/home/sark/dev/python/test'
this_job = 3
num_job = 10
progress = this_job/num_job
job_status = "Running"


print(("Path:{}, Job({}/{}), Progress:{:4.2%}, Status:{}")
      .format(homepath,this_job,num_job,progress,job_status))

์ถœ๋ ฅ๊ฒฐ๊ณผ

Path:/home/sark/dev/python/test, Job(3/10), Progress:30.00%, Status:Running

๊ทธ๋Ÿฐ๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ด„ํ˜ธ๋ฅผ ์ง์ ‘์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ƒฅ ๊ด„ํ˜ธ๋กœ๋Š” ์•„๋ž˜์˜ ๋ฌธ์žฅ์„ ํ‘œ์‹œํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค.

Path:/home/sark/dev/python/test, Job(3/10), Progress:30.00%, Status:Running, Environment Variable:{$JOBPATH}

์ด ๊ฒฝ์šฐ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ด„ํ˜ธ๋ฅผ {{ }} ํ˜•ํƒœ๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์•„์ฃผ ์ข‹์€ ํ•ด๊ฒฐ์ฑ…์ด๋‹ค!

homepath = '/home/sark/dev/python/test'
this_job = 3
num_job = 10
progress = this_job/num_job
job_status = "Running"


print(("Path:{}, Job({}/{}), Progress:{:4.2%}, Status:{},\
 Environment Variable:{{$JOBPATH}}").format(homepath,this_job,num_job,progress,job_status))

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€