Problem in splitting a string

Byron DesertLinux at netscape.net
Thu Jul 22 16:34:31 EDT 2004


HI Angelo,

Here's a function that will take care of what you need:

-----------

	def splitrecord(stringRec):
		theRec = string.split(stringRec, ',')						# Split record into a list.
		name = theRec[14]									   # Store name in memory.
		surname = theRec[15]									# Store surname in memory.
		name = name[1:]										      # Trim quote from name.
		surname = surname[0:len(surname)-1]				     # Trim quote from surname.
		theRec[14] = "%s, %s" % (name, surname)				 # Merge and store name, 
surname in list.
		del(theRec[15])											# Remove list item 15 from list.  Not needed.
		return theRec											 # Return list to user.

-----------

To use this list, do the following:

	recordstring = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
	newRecord = splitrecord(recordstring)
	print newRecord

Result is:

	['', '', '', '', '', '', '23', '', '', 'asd', '', '', '', '', 'name, 
surname', '', '', '', '', '', '', '\n']

Hope this helps,

Byron
---


Angelo Secchi wrote:
> Hi,
> I have string of numbers and words like
> 
> ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
> 
> and I would like to split (I'm using string.split()) it using comma as
> separator but I do not want to split in two also the "name,surname"
> field. In other word I would like python in separating fields to skip
> that particular comma.
> 
> How can I do that?
> 
> Thanks in advance
> 
> Angelo



More information about the Python-list mailing list