Re: simple string question
D'Arcy J.M. Cain wrote: On Mon, 7 Sep 2009 15:29:23 +1000 "jwither" 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) Lots of fun when preceded by: x = "'; sys.exit(); print 'b" or far nastier things. Exec is the same level of dangerous as eval. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string question
On 8 Sep, 05:39, Steven D'Aprano 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
"ryles" 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
Re: simple string question
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
On Mon, 7 Sep 2009 15:29:23 +1000 "jwither" 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 | 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
On 7 Sep, 07:29, "jwither" 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
> 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
"Chris Rebert" wrote in message news:mailman.1075.1252306208.2854.python-l...@python.org... > On Sun, Sep 6, 2009 at 10:29 PM, jwither 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
On Sun, Sep 6, 2009 at 10:29 PM, jwither 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
On Sep 6, 11:29 pm, "jwither" 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
On Sep 6, 10:29 pm, "jwither" 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
simple string question
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
On 28 srp, 14:15, Steve Holden <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > On 28 srp, 07:05, Zentrader <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" > >> If you are doing all of this to format the output into columns, > >> Python's print() or write() will do this, and is easier as well. Some > >> more info on what you want to do will clear things up. > > > Hi, > > > That is confusing me too, so now I will try explain it more.This is > > text before "translation": > > Let me explain you with python code. I want to this "function" act > > code indentation > > Short_Text="n=90; if n==90:print 'ok'" > > Then now I must write that function for detect ";" and ":", and if > > that function detect ";" then it appends "\n" before ";" but > > if detect ":" then it appends "\n\t\t\t\t\t\t\t\t" > Short_text_after_translation="n=90;\nif n==90:\n\t\t\t\t\t\t\t\tprint > 'ok" > > ...And now when we run this code with exec this must look like: > > > n=90; > > if n==90: > >print 'ok' > > > I think this will be enough for help. > > OK, but you don't want that many tab characters if you can the code to > look like you show it. It's not, anyway, a good idea to use tabs to > indent code. > > I suspect what you need is to split the code on semicolons first, then > re-form lines with colons in them. Some simple code to do this would > look *something* like what follows. This will handle a little more than > you wanted. > > >>> Short_Text="n=90; if n==90:print 'ok'" > >>> compound_lines = Short_Text.split(";") > >>> for line in compound_lines: > ... line = line.replace(":", ":\n") > ... print line > ... > n=90 > if n==90: > print 'ok' > >>> > > Note there are issues here that I haven't addressed. The first is that > leading spaces on the second statement need to be removed, and the > second is that this only works at the outermost level of indentation. > For example, if you want to end up translating function definitions with > if statements inside them correctly you will need to handle multiple > levels of indentation. There are other problems, like semicolons and > colons inside string constants should be ignored, but the only way to > get over those will be to parse the program text according to some > grammar rather than using ad-hoc methods such as the above. > > I hope I have finally been of some assistance ... please reply via the > newsgroup, not in personal email. > > regards > Steve > -- > Steve Holden+1 571 484 6266 +1 800 494 3119 > Holden Web LLC/Ltd http://www.holdenweb.com > Skype: holdenweb http://del.icio.us/steve.holden > --- Asciimercial -- > Get on the web: Blog, lens and tag the Internet > Many services currently offer free registration > --- Thank You for Reading - Thanks Steve! When first version of this software emerge (after competition) you will get your full version for free. Once again thanks! Regards, Vedran -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
> >>> Short_Text="n=90; if n==90:print 'ok'" > >>> compound_lines = Short_Text.split(";") > >>> for line in compound_lines: > ... line = line.replace(":", ":\n") > ... print line > ... > n=90 > if n==90: > print 'ok' A variation of this will work if the input file isn't too complicated. I found this link with a Google of "c to python". This will give you an idea of how difficult it can be if you take into account every possibility when converting. You might check the web first, since someone has probably already done what you want. Good luck. http://www.catb.org/~esr/ctopy/ -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
[EMAIL PROTECTED] wrote: > On 28 srp, 07:05, Zentrader <[EMAIL PROTECTED]> wrote: [EMAIL PROTECTED] wrote: > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" >> If you are doing all of this to format the output into columns, >> Python's print() or write() will do this, and is easier as well. Some >> more info on what you want to do will clear things up. > > Hi, > > That is confusing me too, so now I will try explain it more.This is > text before "translation": > Let me explain you with python code. I want to this "function" act > code indentation > Short_Text="n=90; if n==90:print 'ok'" > Then now I must write that function for detect ";" and ":", and if > that function detect ";" then it appends "\n" before ";" but > if detect ":" then it appends "\n\t\t\t\t\t\t\t\t" Short_text_after_translation="n=90;\nif n==90:\n\t\t\t\t\t\t\t\tprint 'ok" > ...And now when we run this code with exec this must look like: > > n=90; > if n==90: >print 'ok' > > I think this will be enough for help. > OK, but you don't want that many tab characters if you can the code to look like you show it. It's not, anyway, a good idea to use tabs to indent code. I suspect what you need is to split the code on semicolons first, then re-form lines with colons in them. Some simple code to do this would look *something* like what follows. This will handle a little more than you wanted. >>> Short_Text="n=90; if n==90:print 'ok'" >>> compound_lines = Short_Text.split(";") >>> for line in compound_lines: ... line = line.replace(":", ":\n") ... print line ... n=90 if n==90: print 'ok' >>> Note there are issues here that I haven't addressed. The first is that leading spaces on the second statement need to be removed, and the second is that this only works at the outermost level of indentation. For example, if you want to end up translating function definitions with if statements inside them correctly you will need to handle multiple levels of indentation. There are other problems, like semicolons and colons inside string constants should be ignored, but the only way to get over those will be to parse the program text according to some grammar rather than using ad-hoc methods such as the above. I hope I have finally been of some assistance ... please reply via the newsgroup, not in personal email. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --- Asciimercial -- Get on the web: Blog, lens and tag the Internet Many services currently offer free registration --- Thank You for Reading - -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
On 28 srp, 07:05, Zentrader <[EMAIL PROTECTED]> wrote: > > > [EMAIL PROTECTED] wrote: > > > > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" > > If you are doing all of this to format the output into columns, > Python's print() or write() will do this, and is easier as well. Some > more info on what you want to do will clear things up. Hi, That is confusing me too, so now I will try explain it more.This is text before "translation": Let me explain you with python code. I want to this "function" act code indentation >>> Short_Text="n=90; if n==90:print 'ok'" Then now I must write that function for detect ";" and ":", and if that function detect ";" then it appends "\n" before ";" but if detect ":" then it appends "\n\t\t\t\t\t\t\t\t" >>> Short_text_after_translation="n=90;\nif n==90:\n\t\t\t\t\t\t\t\tprint 'ok" ...And now when we run this code with exec this must look like: n=90; if n==90: print 'ok' I think this will be enough for help. Regards, Vedran -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
> > [EMAIL PROTECTED] wrote: > > > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" If you are doing all of this to format the output into columns, Python's print() or write() will do this, and is easier as well. Some more info on what you want to do will clear things up. -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
On Jul 27, 11:26 am, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > >> If I understand you correctly you want to replace ";" by ";\n" and ":" > >> by ":\n\t\t\t\t\t\t\t". > >> Well guess what? The replace() method does just this. Have a read: > >> http://docs.python.org/lib/string-methods.html> > > > No,that's not what I need... > > When this function detect ";" or ":" ,it must append character "\n" or > > "\n\t" ahead ":" or ";" another e.g > > > 1) text="Hello world;Hello:Hello2" > > > 2) When function detect ";" or ":" it must append character "\n" or "\n > > \t" ahead ":" or ";", so that must look like this: > > > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" > > Isn't that what I said? > > Please note that appending "\n" to ";" is the very same thing as > replacing ";" with ";\n". > > Also note that the your description of the desired behavior does not > match your example. You say "append "\n\t" after ":"", but thats not > what happens in your example. There you append "\n\t\t\n\n\n\n\n\n" > instead of "\n\t". That is confusing to me. Can you explain? > > /W Confusing to me also. I read this as replace the first ";" with ";/ n", replace the first ":" with "\n\t\t\t\t\t\t\t", and on from there with different requirements. If that is the case then you have to locate the first, second, etc. and add/replace with the appropriate code. String.find and split would have to be used instead of a string.replace if you want to make different changes depending on if it is the first, second..., occurrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
[EMAIL PROTECTED] wrote: >> If I understand you correctly you want to replace ";" by ";\n" and ":" >> by ":\n\t\t\t\t\t\t\t". >> Well guess what? The replace() method does just this. Have a read: >> http://docs.python.org/lib/string-methods.html> >> > No,that's not what I need... > When this function detect ";" or ":" ,it must append character "\n" or > "\n\t" ahead ":" or ";" another e.g > > 1) text="Hello world;Hello:Hello2" > > 2) When function detect ";" or ":" it must append character "\n" or "\n > \t" ahead ":" or ";", so that must look like this: > > NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" > > Isn't that what I said? Please note that appending "\n" to ";" is the very same thing as replacing ";" with ";\n". Also note that the your description of the desired behavior does not match your example. You say "append "\n\t" after ":"", but thats not what happens in your example. There you append "\n\t\t\n\n\n\n\n\n" instead of "\n\t". That is confusing to me. Can you explain? /W -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
On 27 srp, 19:29, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I have one question about string.I am trying to make an function to > > analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", > > if that function in this text find ";" and ":" ( in this example will > > find both) > > > e.g that function must return this: > > > "HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:" > > If I understand you correctly you want to replace ";" by ";\n" and ":" > by ":\n\t\t\t\t\t\t\t". > Well guess what? The replace() method does just this. Have a read: > http://docs.python.org/lib/string-methods.html> > > /W Hello, No,that's not what I need... When this function detect ";" or ":" ,it must append character "\n" or "\n\t" ahead ":" or ";" another e.g 1) text="Hello world;Hello:Hello2" 2) When function detect ";" or ":" it must append character "\n" or "\n \t" ahead ":" or ";", so that must look like this: NEW TEXT : "Hello world;\nHello:\n\t\t\n\n\n\n\n\nHello2" Regards, Vedran -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
[EMAIL PROTECTED] wrote: > I have one question about string.I am trying to make an function to > analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", > if that function in this text find ";" and ":" ( in this example will > find both) > > e.g that function must return this: > > > "HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:" > If I understand you correctly you want to replace ";" by ";\n" and ":" by ":\n\t\t\t\t\t\t\t". Well guess what? The replace() method does just this. Have a read: http://docs.python.org/lib/string-methods.html> /W -- http://mail.python.org/mailman/listinfo/python-list
Re: a simple string question
On Jul 27, 8:23 am, [EMAIL PROTECTED] wrote: > Hello, > > I have one question about string.I am trying to make an function to > analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", > if that function in this text find ";" and ":" ( in this example will > find both) > > e.g that function must return this: > > "HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:" > > Regards, > Vedran You can use split twice print text.split( ":" ) and then split the returned list items on ";". You could also use text.find( ":" ), but that would be pretty much the same thing only harder. Also, you can step through the string one character at a time and compare each character. Finally, you can use an re, (regular expression), but if you are still learning how to parse strings, I don't think you want to get into re's. -- http://mail.python.org/mailman/listinfo/python-list
a simple string question
Hello, I have one question about string.I am trying to make an function to analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", if that function in this text find ";" and ":" ( in this example will find both) e.g that function must return this: "HELLO;\nHELLO2:\n\t\t\t\t\t\t\tWORLD:" Regards, Vedran -- http://mail.python.org/mailman/listinfo/python-list