์ƒˆ์†Œ์‹

์ธ๊ธฐ ๊ฒ€์ƒ‰์–ด

๐Ÿ“„ Python

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

  • -
๋ฐ˜์‘ํ˜•
## ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ–‰(Line) ์ˆซ์ž๋งŒํผ ์ถœ๋ ฅ

inFp = None     # ์ž…๋ ฅ ํŒŒ์ผ ์ถ”๊ธฐํ™”
inStr = ''      # ์ฝ์–ด์˜ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”

inFp = open("D:\data1.txt","r",encoding="utf-8")
# ๋ณ€์ˆ˜์— ์—ด ํŒŒ์ผ ๋‚ด์šฉ ์ €์žฅ, r์€ ์ฝ๊ธฐ๋ชจ๋“œ, ์ธ์ฝ”๋”ฉ์€ ์ €์žฅํ•œ ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ๊ณผ ๋งž์ถฐ์ค˜์•ผํ•จ
# / ์Šฌ๋ž˜์‰ฌ 1๊ฐœ      \\ ์—ญ์Šฌ๋ž˜์‰ฌ 2๊ฐœ  (๊ฒฝ๋กœ ๋ช…์‹œํ•  ๋•Œ )

inStr=inFp.readline()       # ๋ผ์ธ์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ์ค„์”ฉ ์ฝ๊ธฐ
print(inStr,end='')

inStr=inFp.readline()
print(inStr,end='')

inFp.close()

##### ํŒŒ์ผ์— ์žˆ๋Š” ๋ชจ๋“  ๋‚ด์šฉ ์ถœ๋ ฅ
inFp = None     # ์ž…๋ ฅ ํŒŒ์ผ ์ถ”๊ธฐํ™”
inStr = ''      # ์ฝ์–ด์˜ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”

inFp = open("D:\data1.txt","r",encoding="utf-8")

while True :
    inStr = inFp.readline()
    if inStr =="":
        break
    print(inStr,end='')

inFp.close()

##  Quiz ) ๋ผ์ธ ์ œ์ผ ์•ž์— ์ค„ ๋ฒˆํ˜ธ ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ
inFp = None     # ์ž…๋ ฅ ํŒŒ์ผ ์ถ”๊ธฐํ™”
inStr = ''      # ์ฝ์–ด์˜ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”
i=0

inFp = open("D:\data1.txt","r",encoding="utf-8")

while True :
    i=i+1
    inStr = inFp.readline()
    if inStr =="":
        break
    print("%d " %i+inStr,end='')

inFp.close()


### ํ•œ๋ฒˆ์— ๋ชจ๋‘ ์ฝ์–ด ๋“ค์ด๊ธฐ
inFp = None     # ์ž…๋ ฅ ํŒŒ์ผ ์ถ”๊ธฐํ™”
inStr = ''      # ์ฝ์–ด์˜ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”

inFp = open("D:\data1.txt","r",encoding="utf-8")

inStr=inFp.readlines()
print(inStr)
inFp.close()

### ํ•œ๋ฒˆ์— ์ฝ์–ด ๋“ค์ด๊ณ  ํ•œ์ค„์”ฉ ์ถœ๋ ฅ
inFp = None     # ์ž…๋ ฅ ํŒŒ์ผ ์ถ”๊ธฐํ™”
inStr = ''      # ์ฝ์–ด์˜ฌ ๋ฌธ์ž์—ด ์ดˆ๊ธฐํ™”
inList = []

inFp = open("D:\data1.txt","r",encoding="utf-8")

inList=inFp.readlines()

for inStr in inList:
    print(inStr,end='')

inFp.close()

### ํŒŒ์ผ ์•”ํ˜ธํ™” ๋ฐ ์•”ํ˜ธ ํ•ด๋…

print(ord("์ฑˆ"))
# ๋ฌธ์ž๋Š” ์ˆซ์ž ํ˜•ํƒœ๋กœ ์ปดํ“จํ„ฐ์— ์ €์žฅ๋œ๋‹ค
# ํ•œ๊ธ€์€ ์œ ๋‹ˆ์ฝ”๋“œ์•ˆ์— ํฌํ•จ ๋จ

print(chr(52296))
# ์ˆซ์ž๋ฅผ ๋ฌธ์ž ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๋ฉด ์‚ฌ๋žŒ์ด ๋ณด๊ธฐ ํŽธํ•˜๋‹ค

## ์‹œ์ € ์•”ํ˜ธํ™”
# ๋‹จ์ˆœํ•œ +,- ๋กœ ์•”ํ˜ธํ™”, ๋ณตํ˜ธํ™”

num1 = ord("์ฑˆ")+10
print(num1)
print(chr(num1))
# ๋”ํ•˜๊ธฐ ์—ฐ์‚ฐ์œผ๋กœ ์•”ํ˜ธํ™”

num2=num1-10
print(num2)
print(chr(num2))
# ๋นผ๊ธฐ ์—ฐ์‚ฐ์œผ๋กœ ๋ณตํ˜ธํ™”

num1 = (ord("์ฑˆ") * 2)-600
print(num1)
print(chr(num1))

num2 = int((num1+600)/2)
print(chr(num2))



#### ์•”ํ˜ธํ™”, ๋ณตํ˜ธํ™” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ
inFp, outFp = None, None
inStr, outStr = '',''
i = 0
secu = 0

secuYN = input("1.์•”ํ˜ธํ™”  2.๋ณตํ˜ธํ™” // ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜์„ธ์š” : ")
inFname = input("์ž…๋ ฅ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
outFname = input("์ถœ๋ ฅ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")

if secuYN == "1" :
    secu = 100
elif secuYN == "2" :
    secu = -100

inFp = open("d:/"+inFname, 'r', encoding='utf-8')
outFp = open("d:/"+outFname, 'w', encoding='utf-8')

while True :
    inStr = inFp.readline()
    if not inStr :
        break
    outStr = ''

    for i in range(0, len(inStr)) :
        ch = inStr[i]
        chNum = ord(ch)
        chNum = chNum + secu
        # ๋ฌธ์ž ์•”ํ˜ธํ™” or ๋ณตํ˜ธํ™” ์—ฐ์‚ฐ
        ch2 = chr(chNum)
        outStr = outStr + ch2
        # ์•”ํ˜ธํ™” ํ•œ ๋ฌธ์ž๋ฅผ ์ €์žฅ

    outFp.write(outStr)

outFp.close()
inFp. close()

print("%s ==> %s ๋ณ€ํ™˜ ์™„๋ฃŒ" % (inFname, outFname))

# Quiz) ****.txt  => ****_cipher.txt  => ****_nomal.txt
# ํŒŒ์ผ ๋ช… ์ž๋™์ƒ์„ฑ
# ์•”ํ˜ธํ™” ๋ณตํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ณฑํ•˜๊ธฐ ๋‚˜๋ˆ„๊ธฐ ์—ฐ์‚ฐ ๋“ค์–ด๊ฐ€๊ฒŒ
inFp, outFp = None, None
inStr, outStr = '',''
i = 0
secu = 0
cy='_cipher'
no='_nomal'
tx='.txt'

secuYN = input("1.์•”ํ˜ธํ™”  2.๋ณตํ˜ธํ™” // ๋ฒˆํ˜ธ๋ฅผ ์„ ํƒํ•˜์„ธ์š” : ")
inFname = input("์ž…๋ ฅ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")
outFname = input("์ถœ๋ ฅ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” : ")

if secuYN == "1" :
    secu = 300/2*4
    inFp = open("d:/" + inFname+tx, 'r', encoding='utf-8')
    outFp = open("d:/" + outFname+cy+tx, 'w', encoding='utf-8')

elif secuYN == "2" :
    secu = -300/2*4
    inFp = open("d:/" + inFname+tx, 'r', encoding='utf-8')
    outFp = open("d:/" + outFname+no+tx, 'w', encoding='utf-8')

while True :
    inStr = inFp.readline()
    if not inStr :
        break
    outStr = ''

    for i in range(0,len(inStr)) :
        ch = inStr[i]
        chNum = ord(ch)
        chNum = chNum + int(secu)
        # ๋ฌธ์ž ์•”ํ˜ธํ™” or ๋ณตํ˜ธํ™” ์—ฐ์‚ฐ
        ch2 = chr(chNum)
        outStr = outStr + ch2
        # ์•”ํ˜ธํ™” ํ•œ ๋ฌธ์ž๋ฅผ ์ €์žฅ

    outFp.write(outStr)

outFp.close()
inFp. close()

if secuYN=="1":
    print("%s%s ==> %s%s%s ๋ณ€ํ™˜ ์™„๋ฃŒ" % (inFname,tx,outFname,cy,tx))
else:
    print("%s%s ==> %s%s%s ๋ณ€ํ™˜ ์™„๋ฃŒ" % (inFname,tx,outFname,no,tx))


##### ์œˆ๋„์šฐ ์ฐฝ ๋„์šฐ๊ธฐ
from tkinter import *

window = None
canvas = None
XSIZE,YSIZE = 256,256

window = Tk()
canvas = Canvas(window, height=XSIZE, width=YSIZE)

canvas.pack()
window.mainloop()
paper = PhotoImage(width=XSIZE,height=Y)
canvas.create_image((XSIZE/2,YSIZE/2),image=paper,stat="nomal")

๋ฐ˜์‘ํ˜•

'๐Ÿ“„ Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

12. ์›น ํฌ๋กค๋ง  (0) 2022.06.09
11. ํด๋ž˜์Šค  (0) 2022.06.09
9. ๋ชจ๋“ˆ  (0) 2022.06.09
8. ํ•จ์ˆ˜  (0) 2022.06.09
7. ๋ฌธ์ž์—ด  (0) 2022.06.09
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.