views:

101

answers:

3
line = "english: while  french: pendant que  spanish: mientras  german: whrend "

words = line.split('\t')

for each in words:
 each = each.rstrip()

print words

the string in 'line' is tab delimited but also features a single white space character after each translated word, so while split returns the list I'm after, each word annoyingly has a whitespace character at the end of the string.

in the loop I'm trying to go through the list and remove any trailing whitespaces in the strings but it doest seem to work, suggestions?

+1  A: 

Just line.split() could give you stripped words list.

Updating each inside the loop does not make any changes to the words list

Should be done like this

for i in range(len(words)):
    words[i]=words[i].rstrip()

Or

words=map(str.rstrip,words)

See the map docs for details on map.

Or one liner with list comprehension

words=[x.rstrip() for x in line.split("\t")]

Or with regex .findall

words=re.findall("[^\t]+",line)
S.Mark
in the data im parsing that would for example split the french word "pendant que" into two words
dave
Ok, then you could try with my second example `for i in range(len(words)): ...` or `words=map(str.rstrip,words)`
S.Mark
Ah, that works thankyou. I see python shares a few of the benefits of languages like haskell. In the map function is str.strip a constant in this case? Feels odd without a set of parenthesis...
dave
@dave It's the name of the function to be called, with the arguments following. map will use that function on each of the elements in words. So map itself uses funcname( arg ) with funcname given by you, str.rstrip.
extraneon
**downvote**? well, I just wouldn't mind. but would be great if there is an explanation though.
S.Mark
umm, if i was registered to vote it would have been up!Last question: on the str.strip though could you please explain why it isn't string.strip() for example. I thought strip() was a part of the string.py module and is there any other conventions where you would type a method name without parenthesis?
dave
@dave: Type it without parens when you want the function object itself, and with parens when you want the result of the function call.
Ignacio Vazquez-Abrams
+2  A: 
words = line.split('\t')
words = [ i.rstrip() for i in words ]
ghostdog74
thats nice. i didnt realise python did list comprehensions
dave
or you could even combine this into one-liner
SilentGhost
A: 

You can use a regular expression:

import re
words = re.split(r' *\t| +$', line)[:-1]

With this you define the possible sequence as the delimiter. It also allows more than one space because of the * operator (or no space at all).

EDIT: Fixed after Roger Pate pointed an error.

Morgaelyn
You'll still need to remove the last space (the last character in the string) after this.
Roger Pate
You're right. I will fix it.
Morgaelyn