delimiting text file??

Donn Cave donn at u.washington.edu
Thu Oct 14 16:09:41 EDT 1999


Quoth Gerrit Holl <gerrit.holl at pobox.com>:
| Aahz Maruch:
[re how to insert comma delimiters]

|> delimitedString = string[:x] + "," + string[x:]
|> 
|> That's a rather lame way to do it; as you learn more about Python,
|> you'll come across better ways.
|
| Is it? I think I've learned more about Python than Jim has (already finished
| Learning python, and I'm waiting for Programming TkInter with Python now),
| but I don't know a better way than this?

He may have gauged better than I the original poster's zeal for
discovering things on his own, but I was surprised no one mentioned
string.join'ing a list of fields in one step.  It's what I consider
the normal approach to this common task.  Easier on the eyes, probably
faster if there are more than a couple of fields.

  fields = []
  ...
  fields.append(last_field)
  delimitedString = string.join(fields, ',')

Might apply in the poster's situation, hard to say how well.

	Donn Cave, University Computing Services, University of Washington
	donn at u.washington.edu




More information about the Python-list mailing list