Re: Raw Strings with Variables
WilsonOfCanada wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
On Tue, Aug 18, 2009 at 5:51 PM, WilsonOfCanada wrote: > 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 -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
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'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
Dnia 19-08-2009 o 02:09:29 WilsonOfCanada napisał(a): 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 -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
On Tue, Aug 18, 2009 at 5:09 PM, WilsonOfCanada wrote: > 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 -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Strings with Variables
On Tue, 2009-08-18 at 16:16 -0700, WilsonOfCanada wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list