728x90
반응형
SMALL
I/O
앞으로 해도 이효리 뒤로 해도 이효리 이렇게 reverse 해도 같은 문장을 #plaindrome 이라고 부른다.
plaindrome 을 검사하는 연습문제를 만들어 보자. 매우 간단하다.
import re def reverse(text): return text[::-1] def is_plaindrome(text): return text == reverse(text) something = input('enter text:') regexp = '\W' something = re.sub(regexp, '', something) something = something.upper() print(something) if is_plaindrome(something): print('Yes, it is a plaindrome') else: print('No, it is not a plaindrome')Rise to vote, sir. 과 같은 문장은 plaindrome 이다.
정확한 plaindrome 을 확인하기 위해 정규식을 이용하여 영문/숫자가 아닌 경우 제거 하고 모두 대문자로 변경한다.
File I/O 는 정말 쉽다.
python 은 늘 예제 코드를 통해 확인하는 것이 배우기에 쉬운것 같다.
text = 'Programming is fun ' \ 'When the work is dome ' \ 'if you wann make your work also fun:' \ 'use Python!' \ '...' print('---open file for write') f = open('test.txt', 'w') print('---and write text') f.write(text) print('---open file for read') f = open('test.txt', 'r') while True: line = f.readline() if len(line) == 0: break print(line) print('---file close') f.close()쉽다. open (w:write mode, r:read mode, b:binary mode.... help(open)을 통해 자세한 내용을 확인할 수 있다.) 하고 읽고, 쓰고, close 하면된다.
pickle
python 의 객체를 File 로 저장하고 다시 읽을 수 있다. 역시 예제 코드를 통해 확인해 보자.
import pickle shoplistfile = 'shoplist.data' shoplist = ['apple', 'mango', 'carrot'] f = open(shoplistfile, 'wb') pickle.dump(shoplist, f) f.close() del shoplist f = open(shoplistfile, 'rb') storedlist = pickle.load(f) print(storedlist) f.close()
728x90
반응형
LIST
'IT > Python' 카테고리의 다른 글
Python - 예외 처리 (0) | 2018.09.18 |
---|---|
Python - Socket(Client) with struct (쉽게 테스트용 Client 만들기) (0) | 2018.09.11 |
Python - Class (0) | 2018.09.07 |
Python - 자료구조 (0) | 2018.08.30 |
Python - 함수 (0) | 2018.08.27 |