Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Raw strings as input from File?

Reply
Thread Tools

Raw strings as input from File?

 
 
rzed
Guest
Posts: n/a
 
      12-02-2009
utabintarbo <> wrote in
news:adc6c455-5616-471a-8b39-
om:

> I have a log file with full Windows paths on a line. eg:
> K:\A\B\C\10xx\somerandomfilename.ext->/a1/b1/c1/10xx
> \somerandomfilename.ext ; t9999xx; 11/23/2009 15:00:16 ;
> 1259006416
>
> As I try to pull in the line and process it, python changes the
> "\10" to a "\x08". This is before I can do anything with it. Is
> there a way to specify that incoming lines (say, when using
> .readlines() ) should be treated as raw strings?
>
> TIA


Despite all the ragging you're getting, it is a pretty flakey thing
that Python does in this context:
(from a python shell)
>>> x = '\1'
>>> x

'\x01'
>>> x = '\10'
>>> x

'\x08'

If you are pasting your string as a literal, then maybe it does the
same. It still seems weird to me. I can accept that '\1' means x01,
but \10 seems to be expanded to \010 and then translated from octal
to get to x08. That's just strange. I'm sure it's documented
somewhere, but it's not easy to search for.

Oh, and this:
>>> '\7'

'\x07'
>>> '\70'

'8'
.... is realy odd.

--
rzed
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-02-2009
rzed wrote:
> utabintarbo <> wrote in
> news:adc6c455-5616-471a-8b39-
> om:
>
>
>> I have a log file with full Windows paths on a line. eg:
>> K:\A\B\C\10xx\somerandomfilename.ext->/a1/b1/c1/10xx
>> \somerandomfilename.ext ; t9999xx; 11/23/2009 15:00:16 ;
>> 1259006416
>>
>> As I try to pull in the line and process it, python changes the
>> "\10" to a "\x08". This is before I can do anything with it. Is
>> there a way to specify that incoming lines (say, when using
>> .readlines() ) should be treated as raw strings?
>>
>> TIA
>>

>
> Despite all the ragging you're getting, it is a pretty flakey thing
>

When the OP specified readline(), which does *not* behave this way, he
probably deserved what you call "ragging." The backslash escaping is
for string literals, which are in code, not in data files.

In any case, there's a big difference between surprising (to you), and
flakey.
> that Python does in this context:
> (from a python shell)
>
>>>> x = '\1'
>>>> x
>>>>

> '\x01'
>
>>>> x = '\10'
>>>> x
>>>>

> '\x08'
>
> If you are pasting your string as a literal, then maybe it does the
> same. It still seems weird to me. I can accept that '\1' means x01,
> but \10 seems to be expanded to \010 and then translated from octal
> to get to x08. That's just strange. I'm sure it's documented
> somewhere, but it's not easy to search for.
>
>

Check in the help for "escape Strings". It's documented (in vers. 2.6,
anyway) in a nice chart that backslash followed by 3 digits, is
interpreted as octal. I don't like it much either, but it's inherited
from C, which has worked that way for 30+ years.

Online, see
http://www.python.org/doc/2.6.4/refe..._analysis.html, and
look in section 2.4.1 for the chart.
> Oh, and this:
>
>>>> '\7'
>>>>

> '\x07'
>
>>>> '\70'
>>>>

> '8'
> ... is realy odd.
>
>

Octal 70 is hex 38 (or decimal 56), which is the character '8'.

DaveA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Entering strings as user input but interpreting as Python input (sortof) Chris Carlen Python 1 09-18-2007 01:58 AM
Raw strings to normal strings conversion? Nagarajan Python 4 08-23-2007 10:20 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Adobe PS RAW convertor vs Nikon RAW convertor Vlad Gunko Digital Photography 8 01-25-2005 07:43 PM
How raw is RAW format? Editor www.nutritionsoftware.org Digital Photography 4 12-22-2003 07:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57