Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get single \ in python

Tags:

python

I'm trying to learn python, and I'm pretty new at it, and I can't figure this one part out. Basically, what I'm doing now is something that takes the source code of a webpage, and takes out everything that isn't words.

Webpages have a lot of \n and \t, and I want something that will find \ and delete everything between it and the next ' '.

def removebackslash(source):
    while(source.find('\') != -1):
        startback = source.find('\')
        endback = source[startback:].find(' ') + startback + 1
        source = source[0:startback] + source[endback:]
    return source

is what I have. It doesn't work like this, because the \' doesn't close the string, but when I change \ to \\, it interprets the string as \\. I can't figure out anything that is interpreted at '\'

like image 436
fnsjdnfksjdb Avatar asked Jun 07 '12 19:06

fnsjdnfksjdb


2 Answers

\ is an escape character; it either gives characters a special meaning or takes said special meaning away. Right now, it's escaping the closing single quote and treating it as a literal single quote. You need to escape it with itself to insert a literal backslash:

def removebackslash(source):
    while(source.find('\\') != -1):
        startback = source.find('\\')
        endback = source[startback:].find(' ') + startback + 1
        source = source[0:startback] + source[endback:]
    return source
like image 185
Ry- Avatar answered Nov 15 '22 18:11

Ry-


Try using replace:

str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

So in your case:

my_text = my_text.replace('\n', '')
my_text = my_text.replace('\t', '')
like image 32
zallarak Avatar answered Nov 15 '22 18:11

zallarak