trailing newline in string.splitfields() 
Author Message
 trailing newline in string.splitfields()

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



Sat, 19 Feb 2005 22:17:10 GMT  
 trailing newline in string.splitfields()
Did you try regular expressions?

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

HTH
Uwe

Quote:

> 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



Sun, 20 Feb 2005 02:28:22 GMT  
 trailing newline in string.splitfields()

Quote:
> 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.


Sun, 20 Feb 2005 02:33:42 GMT  
 trailing newline in string.splitfields()

Quote:

>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.

AFAICT string.splitfields is now an alias for string.split. The docs say
"""
splitfields(s[, sep[, maxsplit]])
      This function behaves identically to split(). (In the past, split() was only used
      with one argument, while splitfields() was only used with two arguments.)
"""

Quote:
>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])

 ^^^^--you really ought not to use builtin names for variables ;-)

If you are not otherwise using the three var's, you could write

    myList.extend(stringtosplit.strip().split(','))

if you don't care about leading or trailing whitespace. Otherwise, to duplicate
what you are doing, you could write

    myList.extend(stringtosplit[:-1].split(','))

e.g.,

 >>> myList = list('abc')
 >>> myList
 ['a', 'b', 'c']
 >>> stringtosplit = '  <-2sp..1sp-> ,no_spaces,inside spaces and end.\n'
 >>> myList.extend(stringtosplit[:-1].split(','))
 >>> myList
 ['a', 'b', 'c', '  <-2sp..1sp-> ', 'no_spaces', 'inside spaces and end.']

Quote:

>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.

Regular expressions will let you do things like splitting on comma with
optional white space on either side of the comma, but not touching white
space elsewhere. Or ignore delimiters inside of quotations, etc. You might
not need those options every time, but sometimes you do.

Regards,
Bengt Richter



Sun, 20 Feb 2005 07:45:17 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Modifying existing script to remove trailing newlines

2. Remove trailing newlines from file

3. $canvas find enclosed does not find text with trailing newline

4. splitfields(string, "\n")

5. using string.splitfields

6. removing trailing spaces from string variables

7. strings and trailing spaces

8. how to remove trailing blanks of a string?

9. Regular exp matching delimited string excepting trailing delimiters?

10. Strings with newlines

11. Embedding newlines and tabs in strings

12. string.replace() can't replace newline characters???

 

 
Powered by phpBB® Forum Software