Raw Strings with Variables

W

WilsonOfCanada

Hellos,

I know that if you have:

happy = r"C:\moo"
print happy

you get C:\moo instead of C:\\moo

The thing is that I want to do this a variable instead.

ex. testline = fileName.readline()
rawtestline = r testline

Thanks
 
A

Albert Hopkins

Hellos,

I know that if you have:

happy = r"C:\moo"
print happy

you get C:\moo instead of C:\\moo

The thing is that I want to do this a variable instead.

ex. testline = fileName.readline()
rawtestline = r testline

I'm not sure what you are hoping for... Raw strings apply to string
literals. If you are reading from a file, as above, you need not worry
about it:

$ cat test.txt
C:\moo
$ python -c 'r = open("test.txt").readline() ; print r'
C:\moo
 
W

WilsonOfCanada

You're right, but the moment I append it onto a list, it would become
C:\\moo.

arrPlaces = []
intPoint =0

while (len(testline)):
testline = fileName.readline()
print testline
arrPlaces[intPoint].append(testline)
intPoint += 1

print arrPlaces
C:\moo
C:\supermoo
['C:\\moo', 'C:\\supermoo']

Is there a way to stop that when appending to the dictionary?

Thanks
 
C

Chris Rebert

You're right, but the moment I append it onto a list, it would become
C:\\moo.

arrPlaces = []
intPoint =0

while (len(testline)):
       testline = fileName.readline()
       print testline
       arrPlaces[intPoint].append(testline)
       intPoint += 1

print arrPlaces
C:\moo
C:\supermoo
['C:\\moo', 'C:\\supermoo']

That's because the list uses repr() rather than str() to stringify its
items when it is output. repr() shows escape sequences rather than the
literal characters and adds the surrounding quote marks, but the
string is not modified upon placement in the container.

Study this interpreter session:
a = r"C:\supermoo"
b = "\t" #a tab character
c = [a, b]
print a #note the lack of quotes in the output C:\supermoo
print repr(a) #note the quotes and \\ 'C:\\supermoo'
print b #we see the literal tab
print repr(b) #we see the escape sequence '\t'
print c #note how this matches the repr() output from earlier ['C:\\supermoo', '\t']
print c[0], c[1], "end" #but the actual strings are the same as before
C:\supermoo end

Cheers,
Chris
 
J

Jan Kaliszewski

Dnia 19-08-2009 o 02:09:29 WilsonOfCanada said:
You're right, but the moment I append it onto a list, it would become
C:\\moo.

No, it would not. Really!
C:\moo
C:\supermoo
['C:\\moo', 'C:\\supermoo']

It is not the matter of content of the string but only of a way of
*presentation* of it by print:

print arrPlaces

in the example is roughly equivalent to:

print '[' + repr(a_list[0]) + ', ' + repr(a_list[1]) + ']'


So try:

print a_list[0]
Output:
C:\moo

print a_list[1]
Output:
C:\supermoo

print ', '.join(arrPlaces)
Output:
C:\moo, C:\supermoo

print ', '.join("'%s'" % item for item in arrPlaces)
Output:
'C:\moo', 'C:\supermoo'


Cheers,
*j
 
W

WilsonOfCanada

However, when I send the list over as a dictionary for HTML:

d["places"] = arrPlaces

return render_to_response('rentSearch.html', d)

the HTML using Django has:

{{ places }} but returns ['C:\\moo', 'C:\\supermoo']
 
C

Chris Rebert

However, when I send the list over as a dictionary for HTML:

d["places"] = arrPlaces

return render_to_response('rentSearch.html', d)

the HTML using Django has:

{{ places }} but returns ['C:\\moo', 'C:\\supermoo']

As we've explained already, containers (such as dictionaries and
lists) use repr() to display their elements, and repr() for strings
shows the escape sequences (e.g. \\ \t \n) and adds surrounding
quotes. Hence the output you're getting.
If you don't like the way it looks, then either send the list through
a filter that outputs it like you want by accessing the elements
individually, or iterate over the contents of the list in your
template and output it like you want.

Cheers,
Chris
 
T

Terry Reedy

WilsonOfCanada said:
Hellos,

I know that if you have:

happy = r"C:\moo"
print happy

you get C:\moo instead of C:\\moo

The thing is that I want to do this a variable instead.

ex. testline = fileName.readline()
rawtestline = r testline

Python does not have 'raw strings'. It only has 'raw string literals',
which is to say, string literals with 'r' prepended to signal less
processing (cooking) of the literal in the process of turning it into a
string object.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top