Discussion:
[Tutor] Using split with a backslash
Bryan Fodness
2008-04-02 14:44:10 UTC
Permalink
I have a data pair separated by a backslash. I didn' t think it would see
an end of line if the backslash was inside the quotes.
Can this be done? I don't have a choice in what the separator is.
LeafJawPositions='-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\x029.800000000001'
x1, x2 = LeafJawPositions.split('\x0')
ValueError: invalid \x escape
x1, x2 = LeafJawPositions.split('\')
SyntaxError: EOL while scanning single-quoted string
--
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
Dave Kuhlman
2008-04-02 15:40:38 UTC
Permalink
Post by Bryan Fodness
I have a data pair separated by a backslash. I didn' t think it would see
an end of line if the backslash was inside the quotes.
Can this be done? I don't have a choice in what the separator is.
LeafJawPositions='-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\x029.800000000001'
x1, x2 = LeafJawPositions.split('\x0')
ValueError: invalid \x escape
x1, x2 = LeafJawPositions.split('\')
SyntaxError: EOL while scanning single-quoted string
In a literal string, backslash is an escape character. In order to
include a backslash in a literal string, type two backslashes.
Example:

x1, x2 = LeafJawPositions.split('\\')

See http://docs.python.org/ref/strings.html

Another example:

In [1]: a = 'abc\\def\\ghi'
In [2]: len(a)
Out[2]: 11
In [3]: a.split('\')
------------------------------------------------------------
File "<ipython console>", line 1
a.split('\')
^
SyntaxError: EOL while scanning single-quoted string

In [4]: a.split('\\')
Out[4]: ['abc', 'def', 'ghi']


- Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
Sander Sweers
2008-04-02 15:57:17 UTC
Permalink
Post by Bryan Fodness
I have a data pair separated by a backslash. I didn' t think it would see
an end of line if the backslash was inside the quotes.
The backlash is seen as an escape character.

Try the below, notice the string prefix r and that the backslash is
now escaped by another backslash.
Post by Bryan Fodness
LeafJawPositions=r'-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\\29.800000000001'
Post by Bryan Fodness
x1, x2 = LeafJawPositions.split('\\')
x1, x2
('-42.000000000001', '29.800000000001')

See http://docs.python.org/ref/strings.html for more info.

Greets
Sander
Alan Gauld
2008-04-02 15:57:35 UTC
Permalink
Post by Bryan Fodness
I have a data pair separated by a backslash.
I didn' t think it would see an end of line if the backslash
was inside the quotes.
Backslashes don't indicate end of line, they indicate a
continuation of a line. ie they tell Python to *ignore* the
end of line...
Post by Bryan Fodness
Can this be done? I don't have a choice in what the separator is.
Of course.
Post by Bryan Fodness
LeafJawPositions='-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\x029.800000000001'

This is reporting \2 as \x02 - which is one character
Post by Bryan Fodness
LeafJawPositions=r'-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\\29.800000000001'

Note the double \.
Post by Bryan Fodness
LeafJawPositions.split('\\')
['-42.000000000001', '29.800000000001']
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
Bryan Fodness
2008-04-02 16:14:34 UTC
Permalink
Thanks everyone,
I was trying it this way.

x1, x2 = LeafJawPositions.split(r'\\')
Post by Bryan Fodness
LeafJawPositions='-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\x029.800000000001'
x1, x2 = LeafJawPositions.split('\x0')
ValueError: invalid \x escape
x1, x2 = LeafJawPositions.split('\')
SyntaxError: EOL while scanning single-quoted string
Hi,
The backslash is used for escaping special characters in a string. In
order to do what you are trying to do, you would need to escape the
backslash using a backslash.
You need to do this in two places in the above code.
LeafJawPositions='-42.000000000001\\29.800000000001'
and
x1, x2 = LeafJawPositions.split('\\')
http://docs.python.org/ref/strings.html
Regards,
Michael
--
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
Kent Johnson
2008-04-02 16:23:30 UTC
Permalink
Post by Bryan Fodness
Thanks everyone,
I was trying it this way.
x1, x2 = LeafJawPositions.split(r'\\')
That is a string containing *two* backslashes.

Kent
Steve Willoughby
2008-04-02 17:14:09 UTC
Permalink
Post by Bryan Fodness
I have a data pair separated by a backslash. I didn' t think it would see
an end of line if the backslash was inside the quotes.
Can this be done? I don't have a choice in what the separator is.
LeafJawPositions='-42.000000000001\29.800000000001'
LeafJawPositions
'-42.000000000001\x029.800000000001'
x1, x2 = LeafJawPositions.split('\x0')
ValueError: invalid \x escape
x1, x2 = LeafJawPositions.split('\')
Try

x1, x2 = LeafJawPositions.split('\\')
--
Steve Willoughby | Using billion-dollar satellites
***@alchemy.com | to hunt for Tupperware.
Loading...