[Tutor] Using split with a backslash
Alan Gauld
alan.gauld at btinternet.com
Wed Apr 2 17:57:35 CEST 2008
"Bryan Fodness" <bryan.fodness at gmail.com> wrote
> 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...
> Can this be done? I don't have a choice in what the separator is.
Of course.
>>> LeafJawPositions='-42.000000000001\29.800000000001'
>>> LeafJawPositions
'-42.000000000001\x029.800000000001'
This is reporting \2 as \x02 - which is one character
You need to treat the string as raw characters:
>>> LeafJawPositions=r'-42.000000000001\29.800000000001'
>>> LeafJawPositions
'-42.000000000001\\29.800000000001'
Note the double \.
>>> 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
More information about the Tutor
mailing list