Home > Software design >  Extract numbers from string with backslash
Extract numbers from string with backslash

Time:08-13

I need to extract the numbers from a string in the following format: '183\118\40'. I tried:

st = '183\118\40'
re.findall(r"[- ]?(?:\d*\.\d |\d )", st)

output: ['183', '8']

st.split('\\')

output: ['183\t8 ']

CodePudding user response:

You have confused the REPRESENTATION of your string with the CONTENT of your string. The string '183\118\40' contains 6 characters, NONE of which are backslashes. The "\11" is an octal character constant. Octal 11 is decimal 9, which is the tab character. The "\40" is also an octal character constant. Octal 40 is decimal 32, which is space.

If you really want that literal string, you need one of:

st = '183\\118\\40'
st = r'183\118\40'

Note that this only happens because you have typed it as a Python string constant. If you read that line in from file, it will work just fine.

CodePudding user response:

st = '183\118\40'
print(st)

If you try printing this, you will see that this is the output

'183\t8 '

This is not in your required format because of the backslash or escape character() used. To solve this issue, use raw strings. To convert your string into a raw string, do this

st = r'183\118\40'

If you do not want to use raw strings, save it as

st = '183\\118\\40'

If you try printing this, you will see that this is the output

'183\\118\\40'

As you can see, the escape character has been itself escaped by using another escape character after it.

Now, to get the required number values from this, use string manipulation method split() with the delimiter arguement. Like so,

st.split("\\")

Note that here we again escape the escape character with another backslash to set the delimiter as the literal / character.

Now,

print(st)

will give you your required output, that is,

['183', '118', '40']

Now you are ready to further process this list of strings.

If you want a list of integers instead, try

int_list = [int(x) for x in st.split('\\')]

Now visualise the output,

>>>print(int_list)
[183, 118, 40]

CodePudding user response:

It is recommended to use '/xx', str = '\xx' default will be transferred by default.

  • Related