newline backslash dilemma

I’ve appended a directory dialog to a TextCtrl. Only problem is the ‘\n’ includes the backslash.
Output:
a.txt
b.txt
c.txt
d.txt
Using the syntax [:-1] is only extracting the file string. Using .replace(r’\n’,’\n’) has no effect. Perhaps I’m not
implementing this right. Never-the-less I’m still trying to omit the backslash. My code reads:

def OnChooseRoot(self, event):
    dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE)
    if dlg.ShowModal() == wx.ID_OK:
        for dirname, dirnames, filenames in os.walk(dlg.GetPath()):               
            for filename in filenames:
                font1 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Courier New')
                self.middleview.SetFont(font1)
                s=('\n')
                bs=(os.path.join(filename,s))
                self.middleview.AppendText(bs)
                self.bottomview.AppendText(bs)
                print os.path.join(filename)
       
    dlg.Destroy()

From Robin’s quote, do I need to convert to raw strings to do this?
I’m not sure on how to go about this.

Robin:
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).

I've appended a directory dialog to a TextCtrl. Only problem is the '\n'
includes the backslash.
Output:
a.txt\
b.txt\
c.txt\
d.txt\
Using the syntax [:-1] is only extracting the file string. Using
.replace(r'\n','\n') has no effect. Perhaps I'm not
implementing this right. Never-the-less I'm still trying to omit the
backslash. My code reads:

    def OnChooseRoot(self, event):
        dlg = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            for dirname, dirnames, filenames in os.walk(dlg.GetPath()):
                for filename in filenames:
                    font1 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL,
False, u'Courier New')
                    self.middleview.SetFont(font1)
                    s=('\n')
                    bs=(os.path.join(filename,s))
                    self.middleview.AppendText(bs)
                    self.bottomview.AppendText(bs)
                    print os.path.join(filename)

        dlg.Destroy()

From Robin's quote, do I need to convert to raw strings to do this?
I'm not sure on how to go about this.

Robin:
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).

Hi,
in order to represent the backslash character in python string
literal, you have to use an escaped form prefixing it with another
backslash;

"a.txt\\"

'a.txt\\'

print "a.txt\\"

a.txt\

The replacement would be e.g.:

print "a.txt\\".replace("\\","")

a.txt

Your code:
replace(r'\n','\n')
looks for [backslash] folowed by "n", which isn't present in the string.

if you use raw string, you just get separate characters without the
"magic", e.g. [backslash][n]

r'\n'

'\\n'

print r'\n'

\n

as opposed to [newline character] to which the literal "\n" is evaluated:

print '\n'

hopefully, you can adapt it for your usecase,

hth,
   vbr

···

2013/7/25 <georgemccown@gmail.com>:

Thanks vbr, that did it. Modified line to : self.middleview.AppendText(bs.replace(“\”,“”))

···

On Thursday, July 25, 2013 3:47:57 AM UTC-5, vbr wrote:

2013/7/25 george...@gmail.com:

I’ve appended a directory dialog to a TextCtrl. Only problem is the ‘\n’

includes the backslash.

Output:

a.txt\

b.txt\

c.txt\

d.txt\

Using the syntax [:-1] is only extracting the file string. Using

.replace(r’\n’,‘\n’) has no effect. Perhaps I’m not

implementing this right. Never-the-less I’m still trying to omit the

backslash. My code reads:

def OnChooseRoot(self, event):
    dlg = wx.DirDialog(self, "Choose a directory:",

style=wx.DD_DEFAULT_STYLE)

    if dlg.ShowModal() == wx.ID_OK:
        for dirname, dirnames, filenames in os.walk(dlg.GetPath()):
            for filename in filenames:
                font1 = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL,

False, u’Courier New’)

                self.middleview.SetFont(font1)
                s=('\n')
                bs=(os.path.join(filename,s))
                self.middleview.AppendText(bs)
                self.bottomview.AppendText(bs)
                print os.path.join(filename)
    dlg.Destroy()

From Robin’s quote, do I need to convert to raw strings to do this?

I’m not sure on how to go about this.

Robin:

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).

Hi,

in order to represent the backslash character in python string

literal, you have to use an escaped form prefixing it with another

backslash;

“a.txt\”

‘a.txt\’

print “a.txt\”

a.txt\

The replacement would be e.g.:

print “a.txt\”.replace(“\”,“”)

a.txt

Your code:

replace(r’\n’,‘\n’)

looks for [backslash] folowed by “n”, which isn’t present in the string.

if you use raw string, you just get separate characters without the

“magic”, e.g. [backslash][n]

r’\n’

‘\n’

print r’\n’

\n

as opposed to [newline character] to which the literal “\n” is evaluated:

print ‘\n’

hopefully, you can adapt it for your usecase,

hth,

vbr

georgemccown@gmail.com wrote:

I've appended a directory dialog to a TextCtrl. Only problem is the
'\n' includes the backslash.
...
Using the syntax [:-1] is only extracting the file string. Using
.replace(r'\n','\n') has no effect. Perhaps I'm not
implementing this right. Never-the-less I'm still trying to omit the
backslash.

Backslash escapes can be very confusing. One thing to remember is that
backslash escapes are only interpreted in Python source code. They are
not a run-time thing If I write this:
    a = '\n'
that string literal has exactly one character, with ASCII value 0x0A.
It looks like it has two characters, but it does not. It does not
contain a backslash.

But if I do this:
    a = raw_input('Enter something: ')
and then I enter
    \n
that string will have two characters.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.