-1

I want to check a 'x' string whether it is a digit or not in advance.

'1' is naturally a digit.

But and I will use ① what is calld a string number very much.

I don't know the range of string numbers IDE judges as a digit.

'①'.isdigit() returns True.

'⑴'.isdigit() returns True.

'ⅰ' or 'Ⅰ' returns False.

'㈠' returns False. (kanji version of (1) )

'❶' returns True.

I want to do like this.

for s in data:
    if s.isdigit():
       int_ = int(s)

If I accept '①', int will throw an error. Now, I write try:except for it.

Because I'm a japanese, I often use '①' or '⑴'

How to distinguish isdigit or not isdigit in advance?

Should I rely on try:except or counting all of them in advance?

regular expression?

The main problem is I don't know what is judged as a digit.

data = ["1", "23", "345", "①", "(1)", "(2)"]

This data is dynamic value. It will be changed every time.

Moreover, the string like this may expand in the future.

I hope the string of isdigit() == True is accepted by int().

I don't have an urgent problem because of try: except.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Haru
  • 1,884
  • 2
  • 12
  • 30
  • 1
    If the property you're looking to test for is "will `int` accept this string as input", then the way to go about that is to call `int` on it. Methods like `isdigit` test for obscure Unicode character properties that do not correspond to what you're looking for. – user2357112 Mar 03 '22 at 01:45
  • "I don't know the range of string numbers IDE judges as a digit": you don't have to know. It's not the IDE's decision. It is built into the character set of the language. – user207421 Mar 03 '22 at 02:31

1 Answers1

2

I believe that the str.isdecimal method fits your requirements. It excludes strings like '①', but includes other strings like '١' which are accepted by int.

>>> int('١')                
1
duckboycool
  • 2,425
  • 2
  • 8
  • 23
  • But it does *not* accept `'-1'`, which `int` accepts. The whole "test for digits" thing is probably the wrong idea in the first place. – user2357112 Mar 04 '22 at 00:17
  • The question includes string with multiple characters, so negative integers could potentially be possible input. Otherwise, I think that using a method is probably more clear and less clunky than the try except `ValueError` though, so long as there's not other edge cases to worry about. – duckboycool Mar 04 '22 at 03:23