Text
Text Util
외부에서 온 문자열에서 다음과 같은 처리를 합니다.
문자열에서 모두 xxxx를 제거 후 반환 합니다.
def remove_xxxx(text: str, default: Optional[str] = None):
...
문자열 처음과 마지막 에서만 제거 후 반환 합니다.
def strip_xxxx(text: str, default: Optional[str] = None):
...
문자열에서 xxxx 형태를 추출하여 반환 합니다.
def convert_xxxx(text: str, default: Optional[str] = None):
...
문자열에서 xxx를 추출하여 반환 합니다.
def extract_xxxx(text: str, default: Optional[str] = None):
...
- gpp.texts.convert_date(text: str, default: Optional[date] = None) date[source]
text에서 년월일을 추출 합니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[datetime.date]) – 오류 발생시 기본값
- Returns:
년월일
- Return type:
Optional[datetime.date]
- Example:
>>> from gpp.texts import convert_date >>> convert_date('2023-01-01') datetime.date(2023, 1, 1) >>> convert_date('2023.01.01') datetime.date(2023, 1, 1) >>> convert_date('20230101') datetime.date(2023, 1, 1)
- gpp.texts.convert_decimal(text: str, default: Optional[Decimal] = None)[source]
text에서 실수(소수점을 포함)를 추출하여 반환 합니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[Decimal]) – 오류 발생시 기본값
- Returns:
decimal
- Return type:
Optional[Decimal]
- Example:
>>> from gpp.texts import convert_decimal >>> convert_decimal('584.8701610') Decimal('584.8701610') >>> convert_decimal('-123.456789') Decimal('-123.456789')
- gpp.texts.convert_integer(text: str, default: Optional[int] = None)[source]
text에서 정수를 추출하여 반환 합니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[int]) – 오류 발생시 기본값
- Returns:
정수
- Return type:
Optional[int]
- Example:
>>> from gpp.texts import convert_integer >>> convert_integer('584') 584 >>> convert_integer('서울02', 11) 11 >>> convert_integer(' 02') 2
- gpp.texts.convert_money(text: str, unit: int = 1, default: Optional[int] = None) int[source]
text에서 정수를 추출하여 반환 합니다.
- Parameters:
text (str) – 외부 문자열
unit (int) – 단위
default (Optional[int]) – 오류 발생시 기본값
- Returns:
정수
- Return type:
Optional[int]
- Example:
>>> from gpp.texts import convert_money >>> convert_money('10,000', 1000, None) # 단위 천원 10000000
- gpp.texts.convert_readable_count(num: Union[int, float]) str[source]
수량 num을 사람이 읽을 수 있는 문자열로 변환하여 반환 합니다.
- Parameters:
num (Union[int, float) – 수량
- Return type:
str
- Note:
2^10이 아닌, 10^3으로 나눕니다.
- Example:
>>> from gpp.texts import convert_readable_count >>> convert_readable_count(10000) '10.0K' >>> convert_readable_count(1234567890.99) '1.2G' >>> convert_readable_count(1234567890123) '1.2T' >>> convert_readable_count(12345678901234.99) '12.3T' >>> convert_readable_count(123456789012345678.99) '123.5P'
- gpp.texts.convert_readable_filesize(num: Union[int, float], suffix='B') str[source]
파일크기 num을 사람이 읽을 수 있는 문자열로 변환하여 반환 합니다.
- Parameters:
num (Union[int, float) – 파일크기
suffix – 문자열 끝에 붙일 첨자
- Returns:
사람이 읽을 수 있는 문자열
- Return type:
str
- Note:
10^3이 아닌, 2^10으로 나눕니다.
- Example:
>>> from gpp.texts import convert_readable_filesize >>> convert_readable_filesize(10000, 'B') '9.8KiB' >>> convert_readable_filesize(1234567890.99, 'B') '1.1GiB' >>> convert_readable_filesize(12345678901234.99, 'B') '11.2TiB' >>> convert_readable_filesize(123456789012345678.99, 'B') '109.7PiB'
- gpp.texts.convert_readable_timedelta(seconds: int) str[source]
seconds(시간의 초)를 사람이 읽을 수 있는 문자열로 변환하여 반환 합니다.
- Parameters:
seconds (int) – 수량
- Return type:
str
- Note:
소수점은 버림 합니다.
- Example:
>>> from gpp.texts import convert_readable_timedelta >>> convert_readable_timedelta(-1) '1초 전' >>> convert_readable_timedelta(0) '지금' >>> convert_readable_timedelta(1) '1초 후' >>> convert_readable_timedelta(10000) '2시간 후' >>> convert_readable_timedelta(10000000) '3개월 후' >>> convert_readable_timedelta(86400) '1일 후' >>> convert_readable_timedelta(86399) '23시간 후' >>> convert_readable_timedelta(1234567890) '39년 후'
- gpp.texts.convert_words(text: str)[source]
단어들 사이에 있는 중복된 구분자들을 하나로 만듭니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[str]) – 오류 발생시 기본값
- Returns:
변환된 문자열
- Return type:
Optional[str]
- Example:
>>> from gpp.texts import convert_words >>> convert_words('가 나') '가 나'
- gpp.texts.extract_hangul(text: str)[source]
주어진 문자열에서 한글만 추출합니다.
- Parameters:
text (str) – 외부 문자열
- Returns:
변환된 문자열
- Return type:
Optional[str]
- Example:
>>> from gpp.texts import extract_hangul >>> extract_hangul('가abc나') '가나'
- gpp.texts.extract_integer(text: str, default: Optional[int] = None)[source]
text에서 정수를 추출하여 반환 합니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[int]) – 오류 발생시 기본값
- Returns:
정수
- Return type:
Optional[int]
- Example:
>>> from gpp.texts import extract_integer >>> extract_integer('584-87-01610') 5848701610 >>> extract_integer('서울02') 2
- gpp.texts.extract_integer_as_string(text: str, default: Optional[str] = None)[source]
text에서 정수를 추출하여 문자열로 반환 합니다.
- Parameters:
text (str) – 외부 문자열
default (Optional[str]) – 오류 발생시 기본값
- Returns:
정수로 구성된 문자열
- Return type:
Optional[str]
- Example:
>>> from gpp.texts import extract_integer_as_string >>> extract_integer_as_string('584-87-01610') '5848701610' >>> extract_integer_as_string('서울02') '02'
- gpp.texts.remove_spaces(text: str) str[source]
주어진 문자열에서 (’n’, ‘r’, ‘t’, ‘b, ‘u200b’, ‘ ‘ ) 문자들을 제거 합니다.
- Parameters:
text (str) – 외부 문자열
- Returns:
변환된 문자열
- Return type:
Optional[str]
- Example:
>>> from gpp.texts import remove_spaces >>> remove_spaces('a') 'a' >>> remove_spaces(' ') '' >>> remove_spaces(' a ') 'a' >>> remove_spaces(' a ') 'a' >>> remove_spaces('\n\r\t\b \u200b가\n\r\t\b \u200b나\n\r\t\b \u200b다\n\r\t\b \u200b라\n\r\t\b \u200b') '가나다라' >>> remove_spaces(-1) '-1' >>> remove_spaces(1) '1'