trailing newline in string.splitfields()

Uwe Becher uwe_becher at
Tue Sep 3 14:28:22 EDT 2002

Did you try regular expressions?

list = re.split('\s{1,2}',stringtosplit)


Micah Mayo wrote:
> I was splitting up some fields in a textfile using string.split, which 
> works fine, but that it usee ' ' as the delimiter. I've found that some 
> of the things I want to split include a space or two, so I've gone to 
> using string.splitfields, which also works fine, but for one thing. 
> Unlike string.split() it also grabs the newlne('\n') at the end of each 
> line.
> The way I've worked around this is as such:
> var1,var2,var3 = string.splitfields(stringtosplit,',')
> list.append(var1)
> list.append(var2)
> list.append(var3[:-1])
> So, I slice of the character, easy enough. And I'm generally satisfied 
> with that - but I was wondering if there may be, for the cause of more 
> beautiful code, a way to tell string.splitfields() to do it automatically.
> Thanks,
> Micah

More information about the Python-list mailing list