[Python-ideas] min_fields argument to str.split()
Vitor Bosshard
algorias at yahoo.com
Wed Jan 21 02:50:51 CET 2009
----- Mensaje original ----
> De: Calvin Spealman <ironfroggy at gmail.com>
> Para: python-ideas at python.org
> Enviado: martes, 20 de enero, 2009 22:32:21
> Asunto: [Python-ideas] min_fields argument to str.split()
>
> This might be a silly idea, but I was wondering about forcing split()
> to return at least X number of items. For example, I might be getting
> a comma separated string and i want to split it up into names, but I
> might have less than all of them. If it is just 0, 1, or 2, I can use
> partition(), but any more and that doesn't work. Besides, I don't care
> if the separator is there, just to get the values. Might also make
> sense to give the values to give by default.
>
> Example of implementing this:
>
> def split(self, sep=None, max_splits=None, min_items=None):
> parts = self.split(sep, max_splits)
> if len(parts) < min_items:
> parts.extend([None] * (min_items - len(parts)))
> return parts
>
> Use would be like this:
>
> a, b, c, d = "1,2,3".split(',', None, 4)
How about this?
def splitter(string,sep=None,max_splits=-1, min_items=None):
parts = string.split(sep,max_splits)
for part in parts:
yield part
for i in range(min_items-len(parts)):
yield
a, b, c, d = splitter("1,2,3", ",", -1, 4)
Vitor
¡Todo sobre la Liga Mexicana de fútbol! Estadisticas, resultados, calendario, fotos y más:<
http://espanol.sports.yahoo.com/
More information about the Python-ideas
mailing list