Fixed length lists from .split()?
Bob Greschke
bob at passcal.nmt.edu
Fri Jan 26 13:26:46 EST 2007
More information about the Python-list mailing list
Fri Jan 26 13:26:46 EST 2007
- Previous message (by thread): time series data and NumPy
- Next message (by thread): Fixed length lists from .split()?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2007-01-26 11:13:56 -0700, Duncan Booth <duncan.booth at invalid.invalid> said: > Bob Greschke <bob at passcal.nmt.edu> wrote: > >> Is there a fancy way to get Parts=Line.split(";") to make Parts always >> have three items in it, or do I just have to check the length of Parts >> and loop to add the required missing items (this one would just take >> Parts+=[""], but there are other types of lines in the file that have >> about 10 "fields" that also have this problem)? > >>>> def nsplit(s, sep, n): > return (s.split(sep) + [""]*n)[:n] > >>>> nsplit("bcsn; 1000001; 1456", ";", 3) > ['bcsn', ' 1000001', ' 1456'] >>>> nsplit("bcsn; 1000001", ";", 3) > ['bcsn', ' 1000001', ''] That's fancy enough. :) I didn't know you could do [""]*n. I never thought about it before. Thanks! Bob
- Previous message (by thread): time series data and NumPy
- Next message (by thread): Fixed length lists from .split()?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list