14

You create raw string from a string this way:

test_file=open(r'c:\Python27\test.txt','r')

How do you create a raw variable from a string variable, such as

path = 'c:\Python27\test.txt'

test_file=open(rpath,'r')

Because I have a file path:

file_path = "C:\Users\b_zz\Desktop\my_file"

When I do:

data_list = open(os.path.expandvars(file_path),"r").readlines()

I get:

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    scheduled_data_list = open(os.path.expandvars(file_path),"r").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\x08_zz\\Desktop\\my_file'
4
  • why "b_zz" is replaced as "x08_zz" in your error message? Commented Feb 6, 2014 at 14:35
  • 2
    that's what I'd like to know Commented Feb 6, 2014 at 14:40
  • 2
    ´ord('\b')´ is 8. Either double the backslashes or prepend the string in code with an ´r´. Commented Feb 6, 2014 at 14:44
  • Why not just write r"C:\Users\b_zz\Desktop\my_file" in the first place? Or better yet, "C:/Users/b_zz/Desktop/my_file"? Commented Jul 31, 2022 at 4:56

3 Answers 3

11

There is no such thing as "raw string" once the string is created in the process. The "" and r"" ways of specifying the string exist only in the source code itself.

That means "\x01" will create a string consisting of one byte 0x01, but r"\x01" will create a string consisting of 4 bytes '0x5c', '0x78', '0x30', '0x31'. (assuming we're talking about python 2 and ignoring encodings for a while).

You mentioned in the comment that you're taking the string from the user (either gui or console input will work the same here) - in that case string character escapes will not be processed, so there's nothing you have to do about it. You can check it easily like this (or whatever the windows equivalent is, I only speak *nix):

% cat > test <<EOF                                             
heredoc> \x41
heredoc> EOF
% < test python -c "import sys; print sys.stdin.read()"
\x41
Sign up to request clarification or add additional context in comments.

Comments

7

My solution to convert string to raw string (works with this sequences only: '\a', \b', '\f', '\n', '\r', '\t', '\v' . List of all escape sequences is here):

def str_to_raw(s):
    raw_map = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}
    return r''.join(i if ord(i) > 32 else raw_map.get(ord(i), i) for i in s)

Demo:

>>> file_path = "C:\Users\b_zz\Desktop\fy_file"
>>> file_path
'C:\\Users\x08_zz\\Desktop\x0cy_file'
>>> str_to_raw(file_path)
'C:\\Users\\b_zz\\Desktop\\fy_file'

13 Comments

But the I get the path string from a GUI input. How do I add "r" to the beginning?
What the user is asking is, how can i take an unknown string and make it so that the path doesn't get binary-represented (a "raw" string rather than a interpreted string)
In memory there are no raw strings. A raw string is just a helper for source code. If you get the string via (GUI)input everything is OK.
@alwbtc How do you get the path string from the user? If you get a \b character in there, I don't think you got what you wanted from them.
You'll get exactly what the user provided. No string transformation/unescaping happens when you just look at/use a value. The "\b" part is not related to the string itself, but rather it's an artifact of source code parsing.
|
0

The solution by ndpu works for me.

I could not resist the temptation to enhance it (make it compatible with ancient Python 2 versions and hoping to speed it up):

_dRawMap = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}

def getRawGotStr(s):
    #
    return r''.join( [ _dRawMap.get( ord(c), c ) for c in s ] )

I did a careful time trial, and it turns out that the original code by ndpu is a little faster. List comprehensions are fast, but generator expressions are faster.

Comments

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.