Jun Fai Heu
Jun Fai Heu

Reputation: 41

Remove "." and "\" from a string

my project is to capture a log number from Google Sheet using gspread module. But now the problem is that the log number captured is in the form of string ".\1300". I only want the number in the string but I could not remove it using the below code.

Tried using .replace() function to replace "\" with "" but failed.

a='.\1362'
a.replace('\\',"")

Should obtain the string "1362" without the symbol. But the result obtained is ".^2"

Upvotes: 1

Views: 88

Answers (4)

damaredayo
damaredayo

Reputation: 1079

Whats happening here is that \1362 is being encoded as ^2 because of the backslash, so you need to make the string raw before you're able to use it, you can do this by doing

a = r'{}'.format(rawInputString)

or if you're on python3.6+ you can do

a = rf'{rawInputString}'

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386285

When you do a='.\1362', a will only have three bytes:

a = '.\1362'`
print(len(a))  # => 3

That is because \132 represents a single character. If you want to create a six byte string with a dot, a slash, and the digits 1362, you either need to escape the backslash, or create a raw string:

a = r'.\1362'
print(len(a))  # => 6

In either case, calling replace on a string will not replace the characters in that string. a will still be what it was before calling replace. Instead, replace returns a new string:

a = r'.\1362'
b = a.replace('\\', '')
print(a)  # => .\1362
print(b)  # => .1362

So, if you want to replace characters, calling replace is the way to do it, but you've got to save the result in a new variable or overwrite the old.

See String and Bytes literals in the official python documentation for more information.

Upvotes: 1

Daniel Skovli
Daniel Skovli

Reputation: 495

The problem is that \136 has special meaning (similar to \n for newline, \t for tab, etc). Seemingly it represents ^.

Check out the following example:

a = '.\1362'
a = a.replace('\\',"")
print(a)

b = r'.\1362'
b = b.replace('\\',"")
print(b)

Produces

.^2
.\1362

Now, if your Google Sheets module sends .\1362 instead of .\\1362, if is very likely because you are in fact supposed to receive .^2. Or, there's a problem with your character encoding somewhere along the way.

The r modifier I put on the b variable means raw string, meaning Python will not interpret backlashes and leave your string alone. This is only really useful when typing the strings in manually, but you could perhaps try:

a = r'{}'.format(yourStringFromGoogle)

Edit: As pointed out in the comments, the original code did in fact discard the result of the .replace() method. I've updated the code, but please note that the string interpolation issue remains the same.

Upvotes: 2

ExplodingGayFish
ExplodingGayFish

Reputation: 2897

Your string should contains 2 backslashes like this .\\1362 or use r'.\1362' (which is declaring the string as raw and then it will be converted to normal during compile time). If there is only one backslash, Python will understand that \136 mean ^ as you can see (ref: link)

Upvotes: 0

Related Questions