#action Blog 블로그 더하기 ##Blog {{{#!blog hyacinth 2015-02-26T07:34:22 python isnumeric #irccolor 파이썬으로 백엔드를 만드는 중에 cmd 파라미터로 숫자를 받는 부분이 있었는데 [[Code(str.isnumeric())]] 부분에서 음수가 들어올 때 통과를 못했다. 하도 어이가 없어서 여기에 대해 IRC에 던져봤다. {{{#!irccolor [16:15] 18<jaeholee_18> ㅋㅋㅋㅋㅋㅋㅋㅋㅋ [16:17] 20<hyacinth^talk>30 음수 붙은 건 숫자 아닌가요? [16:17] 20<hyacinth^talk>30 print (u"-5").isnumeric() 하니까 [16:17] 20<hyacinth^talk>30 False 내뱉는 거 보니까 [16:17] 20<hyacinth^talk>30 헐.. . [16:18] 18<ditto18> isnumeric 자체가 원래 한 글자에만 먹는거 아닌가요 [16:19] 18<ditto18> Return True if there are only numeric characters in S [16:21] 20<hyacinth^talk>30 5.5 같은 것도 False 떨어지는 거 보니까 [16:21] 20<hyacinth^talk>30 단순히 문자열 처음부터 끝까지 읽어서 0~9 아니면 [16:22] 20<hyacinth^talk>30 False인가보네요 [16:22] 20<hyacinth^talk>30 진짜 숫자 판단하는 건 직접 만들라 이건가 [16:22] 20<hyacinth^talk>30 ... [16:22] 18<lifthrasiir18> float을 통과하면 되죠 뭐 [16:23] 18<lifthrasiir18> 어차피 1e30000 같은 걸 받고 싶은 건 아니잖아요? [16:23] 20<hyacinth^talk>30 아 그래도 되겠구나 [16:23] 18<ditto18> 그리고 isnumeric은 문제가 Ⅲ 같은 것도 [16:23] 18<ditto18> True로 떨어져요 [16:23] 18<ditto18> ㅋㅋㅋ [16:24] 18<kkung18> NUMERIC [16:24] 20<hyacinth^talk>30 ditto//... 진짜네요 [16:24] 18<ditto18> ¾ [16:24] 18<Yurumechan18> 애초에 [16:24] 18<ditto18> 이것도 True ㅋㅋㅋ [16:24] 18<Yurumechan18> 유니코드 문자 체크라서 [16:24] 18<ditto18> 넹 [16:24] 18<Yurumechan18> 별 이상한게 다 잡힘 [16:24] 18<kkung18> 一 이것도 [16:25] 18<kkung18> true인가요 그럼 [16:25] 18<Yurumechan18> isdecimal인가도 마찬가지 [16:25] 18<changwoo18> 네[ 한글도 isalpha() True [16:25] 18<Yurumechan18> isdecimal은 [16:25] 18<Yurumechan18> 아 isdigit에 합쳐져 있나 [16:25] 18<Yurumechan18> 여튼 아랍 문자가 잡힌다거나... [16:25] 18<kkung18> >>> u'一'.isnumeric() [16:25] 18<kkung18> True [16:25] 18<kkung18> 과연 [16:25] 20<hyacinth^talk>30 무섭다 [16:25] 20<hyacinth^talk>30 호러 [16:25] 18<ditto18> ㄲㄲ [16:25] 18<kkung18> >>> u'일'.isnumeric() [16:25] 18<kkung18> False [16:25] 18<kkung18> 차별하네 [16:25] 18<kkung18> (?) [16:25] 18<ditto18> work [16:26] 18<Yurumechan18> 뜻문자의 장... 아니 단점 [16:32] 18<changwoo18> >>> u'𐅉'.isnumeric() [16:32] 18<changwoo18> True [16:32] 18<changwoo18> 꼼꼼하네요 [16:33] 18<ditto18> 저게 무슨 글자죠? ㅠㅠ [16:33] 18<ditto18> greek acrophonic attic ten talents [16:33] 18<changwoo18> 고대그리스숫자..라네요 [17:23] 18<jeyraof^18> 쩐다 [17:23] 18<jeyraof^18> isnumeric() [17:24] 18<kkung18> '一' 집어 넣으면 1 나오는 라이브러리 같은거 만들면 재밌을꺼 같군요 [17:25] 18<kkung18> '一 + 2' 같은걸 만들 수 있을텐데 [17:25] 18<kkung18> 근데 순전히 다 노가다 해야하는건가 [17:25] 18<kkung18> unicode db에 저런건 없나 [17:25] 18<ditto18> 제가 알기론 있는데 [17:25] 18<kkung18> http://www.charbase.com/3192-unicode-ideographic-annotation-one-mark [17:25] 18<kkung18> 그러게요 [17:26] 18<kkung18> 'numeric value' [17:26] 18<ditto18> >>> unicodedata.numeric(u'一') [17:26] 18<ditto18> 1.0 [17:26] 18<ditto18> ㅋㅋㅋㅋ [17:32] 18<토성구름18> https://github.com/karipe/plusplus/blob/master/plusplus.py 이런 쓸데없는 거 만들다가 귀찮아서 버린 적이... }}} > The following methods are present only on unicode objects: > > unicode.isnumeric() > > Return True if there are only numeric characters in S, False otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. > [https://docs.python.org/2/library/stdtypes.html#string-methods Python documentation] http://hyacinth.byus.net/moniwiki/pds/TsukkomiGathering/%ED%97%88%EB%AC%B4_02.jpg?width=300 멍... 파이썬 isnumeric()은 유니코드 상에서 숫자 문자(u'四', u'¾' 분수, u'𐅉' 고대 그리스 숫자 (...) 등)를 True로 판단하는 것이었는데 굉장하긴 하지만 내가 필요했던 건 아니었다. 의미를 잘못 생각했다. 음수까지 대응할 수 있는 대안을 찾아봤는데 형 변환 할 때 예외처리를 사용하는 방법이 나아 보인다. http://stackoverflow.com/questions/736043/checking-if-a-string-can-be-converted-to-float-in-python {{{#!gcode def isfloat(value): try: float(value) return True except ValueError: return False }}} 결과 목록 {{{#!gcode Command to parse Is it a float? Comment -------------------------------------- --------------- ------------ print(isfloat("")) False print(isfloat("1234567")) True print(isfloat("NaN")) True nan is also float print(isfloat("NaNananana BATMAN")) False print(isfloat("123.456")) True print(isfloat("123.E4")) True print(isfloat(".1")) True print(isfloat("1,234")) False print(isfloat("NULL")) False case insensitive print(isfloat(",1")) False print(isfloat("123.EE4")) False print(isfloat("6.523537535629999e-07")) True print(isfloat("6e777777")) True This is same as Inf print(isfloat("-iNF")) True print(isfloat("1.797693e+308")) True print(isfloat("infinity")) True print(isfloat("infinity and BEYOND")) False print(isfloat("12.34.56")) False Two dots not allowed. print(isfloat("#56")) False print(isfloat("56%")) False print(isfloat("0E0")) True print(isfloat("x86E0")) False print(isfloat("86-5")) False print(isfloat("True")) False Boolean is not a float. print(isfloat(True) True Boolean is a float print(isfloat("+1e1^5")) False print(isfloat("+1e1")) True print(isfloat("+1e1.3")) False print(isfloat("+1.3P1")) False print(isfloat("-+1")) False print(isfloat("(1)")) False brackets not interpreted }}} ... }}} [[HTML(