trailing newline in string.splitfields()

Sean 'Shaleh' Perry shalehperry at attbi.com
Tue Sep 3 14:33:42 EDT 2002


On Tuesday 03 September 2002 07:17, 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

there is always rstrip() which removes any trailing whitespace, including the 
newline.




More information about the Python-list mailing list