Domanda

I've encountered a problem in Python when dealing with backward slashes followed by numbers inside a string. I use windows OS environment.

This becomes especially annoying when you have numbers in the beginning of a name in a directory.

Ex: "P:\70_parseFile\80_FileDir\60_FA_050"

This was a discovery for me that you can create special characters if you do "\1", "\2", "\3"... and so on. As wonderful as this seems, I have to ask on how to go about turning this off, or what other different string function is there that doesn't have this special feature?

Thanks, all!

È stato utile?

Soluzione

You have two choices:

  • Backslash those backslashes:

    "P:\\70_parseFile\\80_FileDir\\60_FA_050"
    
  • Use a raw string, in which the backslash loses its "special meaning"

    r"P:\70_parseFile\80_FileDir\60_FA_050"
    
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top