Re: Fixed length lists from .split()?

2007-02-02 Thread George Sakkis
On Feb 1, 2:40 pm, Bob Greschke [EMAIL PROTECTED] wrote: This idiom is what I ended up using (a lot it turns out!): Parts = Line.split(;) Parts += (x-len(Parts))*[] where x knows how long the line should be. If the line already has more parts than x (i.e. [] gets multiplied by a negative

Re: Fixed length lists from .split()?

2007-02-01 Thread Bob Greschke
This idiom is what I ended up using (a lot it turns out!): Parts = Line.split(;) Parts += (x-len(Parts))*[] where x knows how long the line should be. If the line already has more parts than x (i.e. [] gets multiplied by a negative number) nothing seems to happen which is just fine in this

Re: Fixed length lists from .split()?

2007-01-30 Thread Steven Bethard
On Jan 26, 11:07 am, Bob Greschke [EMAIL PROTECTED] wrote: I'm reading a file that has lines like bcsn; 100; 1223 bcsn; 101; 1456 bcsn; 103 bcsn; 110; 4567 The problem is the line with only the one semi-colon. Is there a fancy way to get Parts=Line.split(;)

Re: Fixed length lists from .split()?

2007-01-27 Thread bearophileHUGS
Duncan Booth: def nsplit(s, sep, n): return (s.split(sep) + []*n)[:n] Another version, longer: from itertools import repeat def nsplit(text, sep, n): nsplit(bcsn; 101; 1456, ;, 3) ['bcsn', ' 101', ' 1456'] nsplit(bcsn; 101, ;, 3) ['bcsn', ' 101', '']

Fixed length lists from .split()?

2007-01-26 Thread Bob Greschke
I'm reading a file that has lines like bcsn; 100; 1223 bcsn; 101; 1456 bcsn; 103 bcsn; 110; 4567 The problem is the line with only the one semi-colon. Is there a fancy way to get Parts=Line.split(;) to make Parts always have three items in it, or do I just have

Re: Fixed length lists from .split()?

2007-01-26 Thread Duncan Booth
Bob Greschke [EMAIL PROTECTED] 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

Re: Fixed length lists from .split()?

2007-01-26 Thread Bob Greschke
On 2007-01-26 11:13:56 -0700, Duncan Booth [EMAIL PROTECTED] said: Bob Greschke [EMAIL PROTECTED] 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