-4

I have a problem with the backslash. When the code executes numbers from the text file it looks like this 00744/,00474/ ...

And when I tried to float string into integer I got TypeErro: unsupported operand type(s) for +=: 'int' and 'str'.

How I can remove '/' from the numbers? Thank you

azro
  • 53,056
  • 7
  • 34
  • 70
Kris
  • 1
  • 1

2 Answers2

0

If you have a string and want to remove chars at the end from it, you can use str.rstrip() with custom characters

content = "123/"
print(content.rstrip("/")) # 123

content = "123=/"
print(content.rstrip("/"))      # 123=
print(content.rstrip("/=-_()")) # 123

Then convert to int

int("00474/".rstrip("/"))  # 474
azro
  • 53,056
  • 7
  • 34
  • 70
0

You can you regex re.sub to remove all the backslash.

a = '00744/,00474/,today/'
import re
a = re.sub(r'(?<=\d)\/','',a)
print (a)

The output of this will be:

00744,00474,today/

This will ensure that all backslash removed are only for digits and not for characters.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33