[Tutor] help with re.split use

Lloyd Kvam pythontutor at venix.com
Thu Jan 29 10:50:45 EST 2004


Think you meant:
	somestring.split(' ')
Actually split defaults to ALL whitspace characters  so you can probably omit ' '
and still get what you want.

Daniel Ehrenberg wrote:
> Michel_Bélanger wrote:
> 
>>Hi,
>>
>>I  use the re.split function to parse Fisrt and Last
>>name from a contact 
>>field.  I use the following command:    
>>
>># -*- coding: cp1252 -*-
>>import csv
>>import re
>>row = "Genevieve Camiré"
>>contact = re.split(' ',row)
>>print row, contact, len(contact)
>>
>>This generate the following results:
>>Genevieve Camiré ['Genevieve', 'Camir\xe9'] 2
>>
>>
>>question1:  When I first ran the code, I had and I/O
>>warning message 
>>which  ended up with the addition of the first line
>>of code  # -*- 
>>coding: cp1252 -*-  What is it for?
>>
>>question2:  Why the word 'Camiré' got changed to
>>'Camir\xe9'
>>
>>question3: Some of the row from my contacts list
>>contain only one word 
>>which result with contact been a list of length 1. 
>>Is it possible to 
>>add an argument to the split function so that it
>>generates an empty 
>>string for the second item in contact list: i.e.
>>
>>row = "Belanger"
>>after split function is applied to row
>>contact = ['Belanger','']
>>
>>Thanks
> 
> 
> Don't use re.split(' ', somestring), use
> ' '.split(somestring). That fixes your problem. Only
> use the re module when you're not just matching a
> string.
> 
> Daniel Ehrenberg
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free web site building tool. Try it!
> http://webhosting.yahoo.com/ps/sb/
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list