TheKid
TheKid

Reputation: 53

Split in Python

I have a question regarding a string I am reading and the splitting it based on a delimiter and storing it in an list.

I have this input 1.2.3.4. where the . is the delimiter. now when i use the split I get this character "" at the end. What is this character?

for element in data.split('.'):
    L.append(element)   
    print L  #prints out ["1","2","3","4",""] 

my question is what is the last array element ""? I want to get rid of this element not only in this array but also in the string data. I tried using element.rstrip(element) but its not getting rid of it. I think most of my confusion comes from what "" refers to in python. Ialso tried using L.remove("") but that does not seem to work.

Upvotes: 0

Views: 2531

Answers (4)

alexis
alexis

Reputation: 50190

There is no character in your last result, as others already wrote. "" is the empty string. split() will split at all occurrences of the delimiter, and since there's nothing after the last one, you get that (nothing) in the results.

Rather than remove the trailing dot from your number (which requires you to know that there is a trailing dot), I recommend you just write your code to expect, and discard, empty elements:

for element in data.split('.'):
    if element:
        L.append(element) 

This will also skip empty elements if you have two delimiters in the middle of a string, as in "1.2..3."

Incidentally, you don't need a loop for this. Just assign the result of split, or drop the empty elements using a list comprehension:

L = data.split('.')                           # all parts: equivalent to your code
L = [ elt for elt in data.split('.') if elt ] # non-empty parts only

Upvotes: 1

WeaklyTyped
WeaklyTyped

Reputation: 1341

You get because of trailing dot.Try, before splitting:

L = L[:-1]

For example:

>>> a = "1.2.3.4."
>>> b = a.split('.')
>>> b
['1', '2', '3', '4', '']
>>> b = a[:-1]
>>> b.split('.')
['1', '2', '3', '4']

Upvotes: 0

kindall
kindall

Reputation: 184081

You asked Python to split "1.2.3.4." on ".". There's nothing after the last . so you get an empty element in your list.

Easiest way to prevent that is to strip it off before splitting.

L = data.strip(".").split(".")

(I'm not sure why you're iterating over the list and building up another list from it, when you already have a list.)

Upvotes: 5

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

use strip() to remove the last ., data.strip(".") will do it, or just rstrip("."), if you want to remove only the . on the right side.

In [35]: "1.2.3.4.".strip(".")
Out[35]: '1.2.3.4'
In [36]: "1.2.3.4.".strip(".").split(".")
Out[36]: ['1', '2', '3', '4']

Upvotes: 4

Related Questions