[Tutor] help with re.split use

Daniel Ehrenberg littledanehren at yahoo.com
Thu Jan 29 10:22:20 EST 2004


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/



More information about the Tutor mailing list