wxpython using variable with wx.statictext ignores \n

Hi,
Sorry for cross-posting if you are also subscribed to the python list
I have some wxPython code created with wxGlade that I am customizing.
I have a label created under the def init() section of the Frame Class. It states…
Account
self.Question = wx.StaticText(self, -1, “My question…”)
if I insert a new line character in this string like this then the output is placed on two lines as expected.
self.Question = wx.StaticText(self, -1, “My \nquestion…”)
Output My
question
I am getting the question as an argument using sys.argv[] and putting
it in a variable called “question_text”. The code is as follows…
question_text = “My \nquestion…”
self.Question = wx.StaticText(self, -1, question_text)
Output My \nquestion
How do I get the line text \n recognized so the break is inserted using the variable technique like what happened when I actually used a string?

···


Cheers Simon

Simon Cropper - Open Content Creator / Website Administrator

Free and Open Source Software Workflow Guides


Introduction http://www.fossworkflowguides.com

GIS Packages http://www.fossworkflowguides.com/gis

bash / Python http://www.fossworkflowguides.com/scripting

I am getting the question as an argument using sys.argv and putting it in
a variable called "question_text". The code is as follows...

passing a newline through sys.argv is a trick -- how are you doing that?

if you are doing:

$myscript lineone\nlinetwo

then you are getting this string:

repr(r"lineone\nlinetwo")

"'lineone\\\\nlinetwo'"

when you want this one:

repr(r"lineone\nlinetwo")

"'lineone\\\\nlinetwo'"

in other works, when sys.argv is created, the shell does not interpret
"\n" as a neline, it is simiply legal text.

so you need to process it -- if you only need newlines, I do somethign like:

new_text = sys.argv[1].replace(r'\n','\n')

(note the "r" before the quote, for "raw string" that will keep python
from procssing teh \ as an escape)

-HTH,
  -Chris

···

On Fri, Jun 1, 2012 at 7:38 AM, Simon Cropper <fossworkflowguides@gmail.com> wrote:

    question\_text = &quot;My \\nquestion\.\.\.&quot;
    self\.Question = wx\.StaticText\(self, \-1, question\_text\)

    Output   My \\nquestion

How do I get the line text \n recognized so the break is inserted using the
variable technique like what happened when I actually used a string?

--
Cheers Simon

Simon Cropper - Open Content Creator / Website Administrator

Free and Open Source Software Workflow Guides
------------------------------------------------------------
Introduction http://www.fossworkflowguides.com
GIS Packages http://www.fossworkflowguides.com/gis
bash / Python http://www.fossworkflowguides.com/scripting

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris,

That worked thank you.

I understand what was happening but could not find anything that could undo the effect.

I still don't understand why inserting a raw '/n' into a string which is apparently being treated as a raw string (otherwise why is the /n being ignored in my examples) makes the '/n' active again.

···

On 02/06/12 02:46, Chris Barker wrote:

On Fri, Jun 1, 2012 at 7:38 AM, Simon Cropper > <fossworkflowguides@gmail.com> wrote:

I am getting the question as an argument using sys.argv and putting it in
a variable called "question_text". The code is as follows...

passing a newline through sys.argv is a trick -- how are you doing that?

if you are doing:

$myscript lineone\nlinetwo

then you are getting this string:

repr(r"lineone\nlinetwo")

"'lineone\\\\nlinetwo'"

when you want this one:

repr(r"lineone\nlinetwo")

"'lineone\\\\nlinetwo'"

in other works, when sys.argv is created, the shell does not interpret
"\n" as a neline, it is simiply legal text.

so you need to process it -- if you only need newlines, I do somethign like:

new_text = sys.argv[1].replace(r'\n','\n')

(note the "r" before the quote, for "raw string" that will keep python
from procssing teh \ as an escape)

-HTH,
   -Chris

         question_text = "My \nquestion..."
         self.Question = wx.StaticText(self, -1, question_text)

         Output My \nquestion

How do I get the line text \n recognized so the break is inserted using the
variable technique like what happened when I actually used a string?

--
Cheers Simon

    Simon Cropper - Open Content Creator / Website Administrator

    Free and Open Source Software Workflow Guides
    ------------------------------------------------------------
    Introduction http://www.fossworkflowguides.com
    GIS Packages http://www.fossworkflowguides.com/gis
    bash / Python http://www.fossworkflowguides.com/scripting

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

--
Cheers Simon

    Simon Cropper - Open Content Creator / Website Administrator

    Free and Open Source Software Workflow Guides
    ------------------------------------------------------------
    Introduction http://www.fossworkflowguides.com
    GIS Packages http://gis.fossworkflowguides.com
    bash / Python http://scripting.fossworkflowguides.com

Chris,

That worked thank you.

I understand what was happening but could not find anything that could
undo the effect.

I still don't understand why inserting a raw '/n' into a string which is
apparently being treated as a raw string (otherwise why is the /n being
ignored in my examples) makes the '/n' active again.

A \n inside a string literal is converted by the compiler into a newline character (ASCII 10). If you put the r in front of the string literal then the compiler does not do that, and you then just have \ and n characters in the string.

  >>> s = '\n'
  >>> len(s)
  1
  >>> print repr(s)
  '\n'
  >>> print ord(s)
  10
  >>>
  >>> s = r'\n'
  >>> len(s)
  2
  >>> print s[0], s[1]
  \ n
  >>> print repr(s)
  '\\n'

···

On 6/1/12 8:24 PM, Simon Cropper wrote:
  >>>

--
Robin Dunn
Software Craftsman

Hi Robin,

I understand this, maybe I am not seeing the revise aspects of this problem.

Consider this..

Create a program called test.py with the following code...

     #!/usr/bin/env python

     import sys

     print '1. Unchanged ' + sys.argv[1]
     print '2. Changed with replace ' + sys.argv[1].replace(r'\n','\n')

The output from the command...

     ./test.py "This is a very long string I want to \ninsert a new line character in"

is...

     1. Unchanged This is a very long string I want to \ninsert a new line character in
     2. Changed with replace This is a very long string I want to
     insert a new line character in

OK...

1. Obviously the parameter was converted to or treated as a raw string when transfered to the program. I am making this assumption because the '\n' is ignored and printed in the first print command.
2. The replace command supplied by Chris reads " replace '\n' in sys.argv[1] (which I assume is raw) with the raw string '\n' ".
3. When you do this the '\n' is converted from a raw string to a newline character. I am making this assumption because a newline character is inserted in the output from the second print statement, breaking the output over two lines

Why? How? I am sure I am missing something.

I am using Python 2.7.

···

On 02/06/12 14:13, Robin Dunn wrote:

On 6/1/12 8:24 PM, Simon Cropper wrote:

Chris,

That worked thank you.

I understand what was happening but could not find anything that could
undo the effect.

I still don't understand why inserting a raw '/n' into a string which is
apparently being treated as a raw string (otherwise why is the /n being
ignored in my examples) makes the '/n' active again.

A \n inside a string literal is converted by the compiler into a newline character (ASCII 10). If you put the r in front of the string literal then the compiler does not do that, and you then just have \ and n characters in the string.

>>> s = '\n'
>>> len(s)
1
>>> print repr(s)
'\n'
>>> print ord(s)
10
>>>
>>> s = r'\n'
>>> len(s)
2
>>> print s[0], s[1]
\ n
>>> print repr(s)
'\\n'
>>>

--
Cheers Simon

    Simon Cropper - Open Content Creator / Website Administrator

    Free and Open Source Software Workflow Guides
    ------------------------------------------------------------
    Introduction http://www.fossworkflowguides.com
    GIS Packages http://gis.fossworkflowguides.com
    bash / Python http://scripting.fossworkflowguides.com

The backslash magic happens only in string literals. In other words, when you have a (non-raw) string constant in the source code. In that case the '\n' is written into the string data as the newline character instead of a backslash and an 'n'.

On the other hand, when you have a string value in sys.argv[1] or any other variable it is just data. If that data happens to have a backslash and a 'n' next to each other then that is all it is. Just a backslash and a 'n'. If you replace those two characters with a newline character then the string has a newline in it. A newline character is written as a string literal like '\n' or as chr(10).

···

On 6/1/12 10:24 PM, Simon Cropper wrote:

Hi Robin,

I understand this, maybe I am not seeing the revise aspects of this
problem.

Consider this..

Create a program called test.py with the following code...

#!/usr/bin/env python

import sys

print '1. Unchanged ' + sys.argv[1]
print '2. Changed with replace ' + sys.argv[1].replace(r'\n','\n')

The output from the command...

./test.py "This is a very long string I want to \ninsert a new line
character in"

is...

1. Unchanged This is a very long string I want to \ninsert a new line
character in
2. Changed with replace This is a very long string I want to
insert a new line character in

OK...

1. Obviously the parameter was converted to or treated as a raw string
when transfered to the program. I am making this assumption because the
'\n' is ignored and printed in the first print command.
2. The replace command supplied by Chris reads " replace '\n' in
sys.argv[1] (which I assume is raw) with the raw string '\n' ".
3. When you do this the '\n' is converted from a raw string to a newline
character. I am making this assumption because a newline character is
inserted in the output from the second print statement, breaking the
output over two lines

Why? How? I am sure I am missing something.

--
Robin Dunn
Software Craftsman

Can I paraphrase your response then?

When I replace the raw '\n' into the string value in sys.argv[1], the python interpreter captures it and replaces the raw string with the newline character and inserts it into the string instead.

Personally, I would expect a better syntax would be....

sys.argv[1].replace(r'\n','\n') would literally insert a raw string as asked, and another command say sys.argv[1].replace(cc'\n','\n') could be used to substitute control characters (cc) into a string.

That aside, thanks for the help, sorry it ended up being a python question rather than a wxpython question.

···

On 02/06/12 16:24, Robin Dunn wrote:

On 6/1/12 10:24 PM, Simon Cropper wrote:

Hi Robin,

I understand this, maybe I am not seeing the revise aspects of this
problem.

Consider this..

Create a program called test.py with the following code...

#!/usr/bin/env python

import sys

print '1. Unchanged ' + sys.argv[1]
print '2. Changed with replace ' + sys.argv[1].replace(r'\n','\n')

The output from the command...

./test.py "This is a very long string I want to \ninsert a new line
character in"

is...

1. Unchanged This is a very long string I want to \ninsert a new line
character in
2. Changed with replace This is a very long string I want to
insert a new line character in

OK...

1. Obviously the parameter was converted to or treated as a raw string
when transfered to the program. I am making this assumption because the
'\n' is ignored and printed in the first print command.
2. The replace command supplied by Chris reads " replace '\n' in
sys.argv[1] (which I assume is raw) with the raw string '\n' ".
3. When you do this the '\n' is converted from a raw string to a newline
character. I am making this assumption because a newline character is
inserted in the output from the second print statement, breaking the
output over two lines

Why? How? I am sure I am missing something.

The backslash magic happens only in string literals. In other words, when you have a (non-raw) string constant in the source code. In that case the '\n' is written into the string data as the newline character instead of a backslash and an 'n'.

On the other hand, when you have a string value in sys.argv[1] or any other variable it is just data. If that data happens to have a backslash and a 'n' next to each other then that is all it is. Just a backslash and a 'n'. If you replace those two characters with a newline character then the string has a newline in it. A newline character is written as a string literal like '\n' or as chr(10).

--
Cheers Simon

    Simon Cropper - Open Content Creator / Website Administrator

    Free and Open Source Software Workflow Guides
    ------------------------------------------------------------
    Introduction http://www.fossworkflowguides.com
    GIS Packages http://gis.fossworkflowguides.com
    bash / Python http://scripting.fossworkflowguides.com

:-[ After a bit of reviewing I now realize that the problem is that I was reading the replace function backwards (courtesy of a fellow pythonist and syntax highlighting editor).

The code reads replace(string_to_be_replaced, replacement_string), I was reading it as replace(replacement_string, string_to_be_replaced).

···

On 02/06/12 16:24, Robin Dunn wrote:

On 6/1/12 10:24 PM, Simon Cropper wrote:

Hi Robin,

I understand this, maybe I am not seeing the revise aspects of this
problem.

Consider this..

Create a program called test.py with the following code...

#!/usr/bin/env python

import sys

print '1. Unchanged ' + sys.argv[1]
print '2. Changed with replace ' + sys.argv[1].replace(r'\n','\n')

The output from the command...

./test.py "This is a very long string I want to \ninsert a new line
character in"

is...

1. Unchanged This is a very long string I want to \ninsert a new line
character in
2. Changed with replace This is a very long string I want to
insert a new line character in

OK...

1. Obviously the parameter was converted to or treated as a raw string
when transfered to the program. I am making this assumption because the
'\n' is ignored and printed in the first print command.
2. The replace command supplied by Chris reads " replace '\n' in
sys.argv[1] (which I assume is raw) with the raw string '\n' ".
3. When you do this the '\n' is converted from a raw string to a newline
character. I am making this assumption because a newline character is
inserted in the output from the second print statement, breaking the
output over two lines

Why? How? I am sure I am missing something.

The backslash magic happens only in string literals. In other words, when you have a (non-raw) string constant in the source code. In that case the '\n' is written into the string data as the newline character instead of a backslash and an 'n'.

On the other hand, when you have a string value in sys.argv[1] or any other variable it is just data. If that data happens to have a backslash and a 'n' next to each other then that is all it is. Just a backslash and a 'n'. If you replace those two characters with a newline character then the string has a newline in it. A newline character is written as a string literal like '\n' or as chr(10).

--
Cheers Simon

    Simon Cropper - Open Content Creator / Website Administrator

    Free and Open Source Software Workflow Guides
    ------------------------------------------------------------
    Introductionhttp://www.fossworkflowguides.com
    GIS Packageshttp://gis.fossworkflowguides.com
    bash / Pythonhttp://scripting.fossworkflowguides.com