Python read file as raw string
Given a file contains lines such as: Show
While reading those line, I want the value to be a raw string (unescaped), meaning, in memory, line should be
instead of
Which is escaped when passed over to libs such as sqlobject. For instance, with sqlobject, if I state
I get the desired results while if I state
I do not. So question is basically, I can I pass a raw string when I am not in
declarative/assignment mode (e.g. Thu Apr 8 12:59:05 EDT 2004
Karl Pflästerer said unto the world upon 08/04/2004 12:32: > On 8 Apr 2004, Brian van den Broek <- bvande at po-box.mcgill.ca wrote: > > [...] > >>Why I care: >>I am wanting to store a series of directory names in a plain text >>config file. I can read the file just fine and have can do all I've >>tried with the output of the readline() method so far. But, since I am >>on a Windows machine, I would prefer to turn the readline() output >>into a raw string. Otherwise, the directory separator '\' gets read as >>an escape character. I know I can solve it by using '/' in place of > > > Did you actually try that? Here's a small example: > > $ cat > raw_string > c:\foo\bar\test\new\end > > $ python > Python 2.3.3 (#1, Dec 30 2003, 08:29:25) > [GCC 3.3.1 (cygming special)] on cygwin > Type "help", "copyright", "credits" or "license" for more information. > >>>>f = file('raw_string') >>>>f.readline() > > 'c:\\foo\\bar\\test\\new\\end\r\n' > >>>>f = file('raw_string') >>>>print f.readline() > > c:\foo\bar\test\new\end > > No interpolation happens (\t is tab and \n newline). > > > > Karl Karl, right you are. Thanks. I think I confused myself by assuming that stuff that worked one way in the interactive prompt on strings typed in as literals and assigned to variables would work the same on a string taken from file.readline(). Clearly I need to know more about Python before running around making assumptions! Sorry. On the plus side, I am getting the hang of it, if slowly. Thanks again and best, Brian vdB
More information about the Tutor mailing list Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character. Python Raw StringLet’s say we want to create a string Hi\nHello in python. If we try to assign it to a normal string, the \n will be treated as a new line.
Output:
Let’s see how raw string helps us in treating backslash as a normal character.
Output:
We got the error because python doesn’t know how to decode ‘\x’ as it doesn’t have any special meaning. Let’s see how we can create the same string using raw strings.
If you are on Python console and create a raw-string like below.
Don’t get confused with the output having two backslashes. It’s just to show it as a normal python string where backslash is being escaped. Python Raw String and QuotesWhen a backslash is followed by a quote in a raw string, it’s escaped. However, the backslash also remains in the result. Because of this feature, we can’t create a raw string of single backslash. Also, a raw string can’t have an odd number of backslashes at the end. Some of the invalid raw strings are:
Let’s look at some of the valid raw string examples with quotes.
Output:
That’s all for a quick introduction of python raw string. You can checkout complete python script and more Python examples from our GitHub Repository.
Want to learn more? Join the DigitalOcean Community!Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest. Sign up How do you read a string as raw in Python?Use the built-in function repr() to convert normal strings into raw strings. The string returned by repr() has ' at the beginning and the end. Using slices, you can get the string equivalent to the raw string.
How do I read a text file as string in Python?Python – Read File as String. Open file in read mode. Call inbuilt open() function with file path as argument. open() function returns a file object.. Call read() method on the file object. read() method returns whole content of the file as a string.. Close the file by calling close() method on the file object.. How do you convert a string variable to a raw string in Python?In Python, when you prefix a string with the letter r or R such as r'...' and R'...' , that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters.
What does \r do in Python?In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.
|