0

I wrote this simple little program:

def main ( ):
    with open("test.txt", "rt") as fin:
        with open("out.txt", "wt") as fout:
                for line in fin:
                    fout.write(line.replace("\", "/"))
    print ("done")
main ()

I know that "" is an escape literal in Python but all I need is to scan through the text file and replace every single backlash with a forward slash "/".

Anyone knows what to do?

2

1 Answer 1

1

You have to remember that strings in python are interpreted. Only raw strings do not follow this rule. Here I mean that if for example you have a "\n" included in your string, it would be interpreted as a new line. Fortunately strings read from file are already raw.

All you have to do is simply use the regular expression:

s.replace('\\','/')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! that seems to solve the issue I was having.
You know how to replace '\"' by 'somestring' as in ... 'hello \" to replace'.replace('\"', '\\"') ... This does not seem to work.
Use cascade of replacements: 'hello \" to replace'.replace('\\', '').replace('"', '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.