Python - 처음 시작하는 파이썬 12장 데이터 길들이기 + 연습문제
학습내용 💩
연습문제
12.1
유니코드 문자열 변수 mystery를 생성하고, 여기에 값 ‘\U0001f4a9’를 할당한다. mystery와 mystery에 대한 유니코드 이름을 찾아서 출력한다.
>>> import unicodedata
>>> mystery = '\U0001f4a9'
>>> unicodedata.name(mystery)
'PILE OF POO'
>>> mystery
>>> '💩'
(….?)
12.2
mystery를 인코딩해보자. 이번에는 UTF-8로 바이트 변수 pop_bytes에 할당하고, 이를 출력한다.
>>> mystery = '\U0001f4a9'
>>> pop_bytes = mystery.encode('utf-8')
>>> pop_bytes
b'\xf0\x9f\x92\xa9'
12.3
UTF-8을 이용하여 pop_bytes를 문자열 변수 pop_string에 디코딩하여 출력한다. pop_string은 mystery와 같은가? 답:같다
>>> pop_string = pop_bytes.decode('utf-8')
>>> pop_string
'💩'
12.4
정규 표현식은 텍스트 작업에 매우 편리하다. mammoth.txt에 정규 표현식을 적용해보자. 텍스트 문자열을 mammoth 변수에 할당한다. 사이트 텍스트파일
>>> mammoth = """
... We have seen thee, queen of cheese,
... Lying quietly at your ease,
... Gently fanned by evening breeze,
... Thy fair form no flies dare seize.
...
... All gaily dressed soon you'll go
... To the great Provincial show,
... To be admired by many a beau
... In the city of Toronto.
...
... Cows numerous as a swarm of bees,
... Or as the leaves upon the trees,
... It did require to make thee please,
... And stand unrivalled, queen of cheese.
...
... May you not receive a scar as
... We have heard that Mr. Harris
... Intends to send you off as far as
... The great world's show at Paris.
...
... Of the youth beware of these,
... For some of them might rudely squeeze
... And bite your cheek, then songs or glees
... We could not sing, oh! queen of cheese.
...
... We'rt thou suspended from balloon,
... You'd cast a shade even at noon,
... Folks would think it was the moon
... About to fall and crush them soon."""
12.5
파이썬의 정규 표현식 함수를 사용하기 위해 re모듈을 임포트한다. c로 시작하는 단어를 모두 출력하기 위해 re.findall()을 사용한다.
>>> result = re.findall('c',mammoth)
>>> result
['c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c']
>>>
12.6
c로 시작하는 네 글자의 단어를 모두 찾는다.
댓글남기기