Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Raw strings as input from File? (http://www.velocityreviews.com/forums/t706469-raw-strings-as-input-from-file.html)

utabintarbo 11-24-2009 07:52 PM

Raw strings as input from File?
 
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

MRAB 11-24-2009 08:27 PM

Re: Raw strings as input from File?
 
utabintarbo wrote:
> 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?
>

..readlines() doesn't change the "\10" in a file to "\x08" in the string
it returns.

Could you provide some code which shows your problem?

Carsten Haese 11-24-2009 08:28 PM

Re: Raw strings as input from File?
 
utabintarbo wrote:
> 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".


Python does no such thing. When Python reads bytes from a file, it
doesn't interpret or change those bytes in any way. Either there is
something else going on here that you're not telling us, or the file
doesn't contain what you think it contains. Please show us the exact
code you're using to process this file, and show us the exact contents
of the file you're processing.

--
Carsten Haese
http://informixdb.sourceforge.net


utabintarbo 11-24-2009 09:20 PM

Re: Raw strings as input from File?
 
On Nov 24, 3:27*pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
>
> .readlines() doesn't change the "\10" in a file to "\x08" in the string
> it returns.
>
> Could you provide some code which shows your problem?


Here is the code block I have so far:
for l in open(CONTENTS, 'r').readlines():
f = os.path.splitext(os.path.split(l.split('->')[0]))[0]
if f in os.listdir(DIR1) and os.path.isdir(os.path.join(DIR1,f)):
shutil.rmtree(os.path.join(DIR1,f))
if f in os.listdir(DIR2) and os.path.isdir(os.path.join(DIR2,f)):
shutil.rmtree(os.path.join(DIR2,f))

I am trying to find dirs with the basename of the initial path less
the extension in both DIR1 and DIR2

A minimally obfuscated line from the log file:
K:\sm\SMI\des\RS\Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz->/arch_m1/
smi/des/RS/Pat/10DJ/121.D5-30\1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
11/24/2009 08:16:42 ; 1259068602

What I get from the debugger/python shell:
'K:\\sm\\SMI\\des\\RS\\Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz->/arch_m1/
smi/des/RS/Pat/10DJ/121.D5-30/1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
11/24/2009 08:16:42 ; 1259068602'

TIA


Jon Clements 11-24-2009 09:50 PM

Re: Raw strings as input from File?
 
On Nov 24, 9:20*pm, utabintarbo <utabinta...@gmail.com> wrote:
> On Nov 24, 3:27*pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
>
>
>
> > .readlines() doesn't change the "\10" in a file to "\x08" in the string
> > it returns.

>
> > Could you provide some code which shows your problem?

>
> Here is the code block I have so far:
> for l in open(CONTENTS, 'r').readlines():
> * * f = os.path.splitext(os.path.split(l.split('->')[0]))[0]
> * * if f in os.listdir(DIR1) and os.path.isdir(os.path.join(DIR1,f)):
> * * * * shutil.rmtree(os.path.join(DIR1,f))
> * * * * if f in os.listdir(DIR2) and os.path.isdir(os.path.join(DIR2,f)):
> * * * * * * * * shutil.rmtree(os.path.join(DIR2,f))
>
> I am trying to find dirs with the basename of the initial path less
> the extension in both DIR1 and DIR2
>
> A minimally obfuscated line from the log file:
> K:\sm\SMI\des\RS\Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz->/arch_m1/
> smi/des/RS/Pat/10DJ/121.D5-30\1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
> 11/24/2009 08:16:42 ; 1259068602
>
> What I get from the debugger/python shell:
> 'K:\\sm\\SMI\\des\\RS\\Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz->/arch_m1/
> smi/des/RS/Pat/10DJ/121.D5-30/1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
> 11/24/2009 08:16:42 ; 1259068602'
>
> TIA


jon@jon-desktop:~/pytest$ cat log.txt
K:\sm\SMI\des\RS\Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz->/arch_m1/
smi/des/RS/Pat/10DJ/121.D5-30\1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
11/24/2009 08:16:42 ; 1259068602

>>> log = open('/home/jon/pytest/log.txt', 'r').readlines()
>>> log

['K:\\sm\\SMI\\des\\RS\\Pat\\10DJ\\121.D5-30\\1215B-B-D5-BSHOE-MM.smz-
>/arch_m1/\n', 'smi/des/RS/Pat/10DJ/121.D5-30\\1215B-B-D5-BSHOE-

MM.smz ; t9480rc ;\n', '11/24/2009 08:16:42 ; 1259068602\n']

See -- it's not doing anything :)

Although, "Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz" and "Pat
\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz" seem to be fairly different -- are
you sure you're posting the correct output!?

Jon.

Jon Clements 11-24-2009 09:54 PM

Re: Raw strings as input from File?
 
On Nov 24, 9:50*pm, Jon Clements <jon...@googlemail.com> wrote:
> On Nov 24, 9:20*pm, utabintarbo <utabinta...@gmail.com> wrote:

[snip]
> Although, "Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz" and "Pat
> \x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz" seem to be fairly different -- are
> you sure you're posting the correct output!?
>


Ugh... let's try that...

Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz
Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz

Jon.

Terry Reedy 11-24-2009 11:06 PM

Re: Raw strings as input from File?
 
utabintarbo wrote:
> 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 should only happen if you paste the test into your .py file as a
string literal.

> 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?


Or if you use execfile or compile and ask Python to interprete the input
as code.

There are no raw strings, only raw string code literals marked with an
'r' prefix for raw processing of the quoted text.


Grant Edwards 11-25-2009 03:31 AM

Re: Raw strings as input from File?
 
On 2009-11-25, Rhodri James <rhodri@wildebst.demon.co.uk> wrote:
> On Tue, 24 Nov 2009 21:20:25 -0000, utabintarbo <utabintarbo@gmail.com>
> wrote:
>
>> On Nov 24, 3:27 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
>>>
>>> .readlines() doesn't change the "\10" in a file to "\x08" in the string
>>> it returns.
>>>
>>> Could you provide some code which shows your problem?

>>
>> Here is the code block I have so far:
>> for l in open(CONTENTS, 'r').readlines():
>> f = os.path.splitext(os.path.split(l.split('->')[0]))[0]
>> if f in os.listdir(DIR1) and os.path.isdir(os.path.join(DIR1,f)):
>> shutil.rmtree(os.path.join(DIR1,f))
>> if f in os.listdir(DIR2) and os.path.isdir(os.path.join(DIR2,f)):
>> shutil.rmtree(os.path.join(DIR2,f))

>
> Ahem. This doesn't run. os.path.split() returns a tuple, and calling
> os.path.splitext() doesn't work. Given that replacing the entire loop
> contents with "print l" readily disproves your assertion, I suggest you
> cut and paste actual code if you want an answer. Otherwise we're just
> going to keep saying "No, it doesn't", because no, it doesn't.


It's, um, rewarding to see my recent set of instructions being
followed.

>> A minimally obfuscated line from the log file:
>> K:\sm\SMI\des\RS\Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz->/arch_m1/
>> smi/des/RS/Pat/10DJ/121.D5-30\1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
>> 11/24/2009 08:16:42 ; 1259068602
>>
>> What I get from the debugger/python shell:
>> 'K:\\sm\\SMI\\des\\RS\\Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz->/arch_m1/
>> smi/des/RS/Pat/10DJ/121.D5-30/1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
>> 11/24/2009 08:16:42 ; 1259068602'

>
> When you do what, exactly?


;)

--
Grant

Dennis Lee Bieber 11-25-2009 05:44 AM

Re: Raw strings as input from File?
 
On Tue, 24 Nov 2009 13:20:25 -0800 (PST), utabintarbo
<utabintarbo@gmail.com> declaimed the following in
gmane.comp.python.general:

>
> Here is the code block I have so far:
> for l in open(CONTENTS, 'r').readlines():
> f = os.path.splitext(os.path.split(l.split('->')[0]))[0]
> if f in os.listdir(DIR1) and os.path.isdir(os.path.join(DIR1,f)):
> shutil.rmtree(os.path.join(DIR1,f))
> if f in os.listdir(DIR2) and os.path.isdir(os.path.join(DIR2,f)):
> shutil.rmtree(os.path.join(DIR2,f))
>
> I am trying to find dirs with the basename of the initial path less
> the extension in both DIR1 and DIR2
>

And just what are DIR1 and DIR2?

So far as I can tell, the likely position of your problem is that
THEY are the source of the problem, and you are joining them to a
perfectly valid item.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/


Jon Clements 11-25-2009 12:58 PM

Re: Raw strings as input from File?
 
On Nov 25, 3:31*am, Grant Edwards <inva...@invalid.invalid> wrote:
> On 2009-11-25, Rhodri James <rho...@wildebst.demon.co.uk> wrote:
>
>
>
> > On Tue, 24 Nov 2009 21:20:25 -0000, utabintarbo <utabinta...@gmail.com> *
> > wrote:

>
> >> On Nov 24, 3:27 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:

>
> >>> .readlines() doesn't change the "\10" in a file to "\x08" in the string
> >>> it returns.

>
> >>> Could you provide some code which shows your problem?

>
> >> Here is the code block I have so far:
> >> for l in open(CONTENTS, 'r').readlines():
> >> * * f = os.path.splitext(os.path.split(l.split('->')[0]))[0]
> >> * * if f in os.listdir(DIR1) and os.path.isdir(os.path.join(DIR1,f)):
> >> * * * * shutil.rmtree(os.path.join(DIR1,f))
> >> * * * * if f in os.listdir(DIR2) and os.path.isdir(os.path.join(DIR2,f)):
> >> * * * * * * shutil.rmtree(os.path.join(DIR2,f))

>
> > Ahem. *This doesn't run. *os.path.split() returns a tuple, and calling *
> > os.path.splitext() doesn't work. *Given that replacing the entire loop *
> > contents with "print l" readily disproves your assertion, I suggest you *
> > cut and paste actual code if you want an answer. *Otherwise we're just *
> > going to keep saying "No, it doesn't", because no, it doesn't.

>
> It's, um, rewarding to see my recent set of instructions being
> followed.
>
> >> A minimally obfuscated line from the log file:
> >> K:\sm\SMI\des\RS\Pat\10DJ\121.D5-30\1215B-B-D5-BSHOE-MM.smz->/arch_m1/
> >> smi/des/RS/Pat/10DJ/121.D5-30\1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
> >> 11/24/2009 08:16:42 ; 1259068602

>
> >> What I get from the debugger/python shell:
> >> 'K:\\sm\\SMI\\des\\RS\\Pat\x08DJQ.D5-30Q5B-B-D5-BSHOE-MM.smz->/arch_m1/
> >> smi/des/RS/Pat/10DJ/121.D5-30/1215B-B-D5-BSHOE-MM.smz ; t9480rc ;
> >> 11/24/2009 08:16:42 ; 1259068602'

>
> > When you do what, exactly?

>
> ;)
>
> --
> Grant


Can't remember if this thread counts as "Edwards' Law 5[b|c]" :)

I'm sure I pinned it up on my wall somewhere, right next to
http://imgs.xkcd.com/comics/tech_sup...heat_sheet.png

Jon.


All times are GMT. The time now is 07:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.