Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create raw string from string variable in python?

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'
like image 942
alwbtc Avatar asked Feb 06 '14 14:02

alwbtc


Video Answer


1 Answers

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
like image 93
viraptor Avatar answered Oct 20 '22 09:10

viraptor