[Tutor] help with re.split use

Michel Bélanger michel.belanger at seidel.ca
Thu Jan 29 11:06:07 EST 2004


Yes, it did work.

Now When I run the following code I get:

# -*- coding: cp1252 -*-
row = "Genevieve Camirée"
contact = row.split()
print row, contact

Genevieve Camirée ['Genevieve', 'Camir\xe9e']

Do you have any idea about the 'é' why it get modify to '\xe9e' in the 
process?

Lloyd Kvam wrote:

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





More information about the Tutor mailing list