Re: simple string question

2009-09-08 Thread Niklas Norrthon
On 8 Sep, 05:39, Steven D'Aprano
ste...@remove.this.cybersource.com.au wrote:
 On Mon, 07 Sep 2009 01:54:09 -0700, Niklas Norrthon wrote:
  Others have answered how to replace '\\n' with '\n'. For a more general
  approach which will handle all string escape sequences allowed in python
  (including '\xdd' and similar), python's eval can be used:

 eval can do so much more than handle escape sequences:

Yes, eval is really cool :-)

 quoted_string = ') or __import__(os).system(echo \'Pwn3d\';#rm -rf /'
 print eval('str(%s)' % quoted_string)

 Every (bad) programmer should pass untrusted strings to eval as a quick
 and unsafe way to do trivial transformations.

It all depends on the origin of the strings of course.

I must admit that I didn't think of str.decode('string_escape') which
of course is the correct way to solve the problem (after inspecting
a sample of the input data to make sure it conforms to the
specification, and isn't rtf or some such).

I probably should decrease the volume of quick and dirty one time
hacks I produce...

/Niklas

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread Sean DiZazzo
On Sep 6, 10:29 pm, jwither jwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences,
 (specifically, slash n), what is the best way to convert that to a parsed
 string, where the escape sequence has been replaced (specifically, by a
 NEWLINE token)?

 James Withers

I believe \n is a newline.  As is \r\n and \r.  Choose your
demon.

mystring = mystring.replace(\n, demon)

FYI.  If you are reading from a file, you can iterate over the lines
without having to worry about newlines:

fi = open(path_to_file, 'r')

for line in fi:
process_line(line)

~Sean
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread 7stud
On Sep 6, 11:29 pm, jwither jwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences,
 (specifically, slash n), what is the best way to convert that to a parsed
 string, where the escape sequence has been replaced (specifically, by a
 NEWLINE token)?

 James Withers

1) What is a parsed string?
2) What is a NEWLINE token?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread Chris Rebert
On Sun, Sep 6, 2009 at 10:29 PM, jwitherjwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences,
 (specifically, slash n), what is the best way to convert that to a parsed
 string, where the escape sequence has been replaced (specifically, by a
 NEWLINE token)?

There's probably a more general method covering all the escape
sequences, but for just \n:

your_string = your_string.replace(\\n, \n)

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread jwither

Chris Rebert c...@rebertia.com wrote in message 
news:mailman.1075.1252306208.2854.python-l...@python.org...
 On Sun, Sep 6, 2009 at 10:29 PM, jwitherjwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences,
 (specifically, slash n), what is the best way to convert that to a parsed
 string, where the escape sequence has been replaced (specifically, by a
 NEWLINE token)?

 There's probably a more general method covering all the escape
 sequences, but for just \n:

 your_string = your_string.replace(\\n, \n)

 Cheers,
 Chris
 --
 http://blog.rebertia.com


Thanks!  (the others are more likely to be errors than deliberate anyway)

James Withers 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread ryles

 There's probably a more general method covering all the escape
 sequences, but for just \n:

 your_string = your_string.replace(\\n, \n)

py s = hello\\r\\n
py s
'hello\\r\\n'
py s.decode(string_escape)
'hello\r\n'
py

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread Niklas Norrthon
On 7 Sep, 07:29, jwither jwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences,
 (specifically, slash n), what is the best way to convert that to a parsed
 string, where the escape sequence has been replaced (specifically, by a
 NEWLINE token)?

 James Withers

Others have answered how to replace '\\n' with '\n'. For a more
general approach which will handle all string escape sequences allowed
in python (including '\xdd' and similar), python's eval can be used:

 quoted_string = 'hello\\nworld\\x21\\tand\\tgood\\040\\x47ood bye!'
 print quoted_string
'hello\nworld\x21\tAnd\tgood\040\x47ood bye!'
 print eval('str(%s)' % quoted_string)
hello
world!  And good Good bye!

If the string isn't quoted just enclosed it in quotes first:
 unquoted_string = 'hello\\nworld\\x21\\tand\\tgood\\040\\x47ood bye!'
 print unquoted_string
hello\nworld\x21\tAnd\tgood\040\x47ood bye!
 print eval('str(%s)' % unquoted_string)
hello
world!  And good Good bye!

/Niklas Norrthon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread D'Arcy J.M. Cain
On Mon, 7 Sep 2009 15:29:23 +1000
jwither jwit...@sxder4kmju.com wrote:
 Given a string (read from a file) which contains raw escape sequences, 
 (specifically, slash n), what is the best way to convert that to a parsed 
 string, where the escape sequence has been replaced (specifically, by a 
 NEWLINE token)?

I don't know what your actual requirement is but maybe this fits:

exec(print '%s' % x)

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread Steven D'Aprano
On Mon, 07 Sep 2009 01:54:09 -0700, Niklas Norrthon wrote:

 Others have answered how to replace '\\n' with '\n'. For a more general
 approach which will handle all string escape sequences allowed in python
 (including '\xdd' and similar), python's eval can be used:

eval can do so much more than handle escape sequences:


quoted_string = ') or __import__(os).system(echo \'Pwn3d\';#rm -rf /'
print eval('str(%s)' % quoted_string)

Every (bad) programmer should pass untrusted strings to eval as a quick 
and unsafe way to do trivial transformations.

http://en.wikipedia.org/wiki/Code_injection



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string question

2009-09-07 Thread jwither

ryles ryle...@gmail.com wrote in message 
news:b96be200-9762-4f92-bd0d-9be076bcd...@y20g2000vbk.googlegroups.com...

 There's probably a more general method covering all the escape
 sequences, but for just \n:

 your_string = your_string.replace(\\n, \n)

 py s = hello\\r\\n
 py s
 'hello\\r\\n'
 py s.decode(string_escape)
 'hello\r\n'
 py


Even though that's what I asked for, I'll stick with the replace for now.
But it's cool though: I can embed generic uni-code as well as simple escape 
sequences!

Thanks,
James Withers. 


-- 
http://mail.python.org/mailman/listinfo/python-list