Cazz
Cazz

Reputation: 61

Python split string that have two delimited

I want to know how to split a string with more then one delimiter. I have a problem splitting if one is space?

I trying to read a text file that has this:

22.0;2016-01-16 00:16:18

I know how to read a text file to a variable, but when I have problem to split even a string I can't go further.

All I have now is this code:

with open('datasource_3.txt', 'r') as f:
     data = f.readlines()
     for line in data:
         words = line.strip().split(';')
    print words

Upvotes: 0

Views: 125

Answers (4)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You can use shlex to create your own parser:

import shlex

def split_multi(_str, delims="", ws=True):
    parser = shlex.shlex(_str, posix=True)
    parser.whitespace_split = ws
    parser.whitespace += delims
    return pars

Demo:

In [15]: sh = split_multi(s, ";")

In [16]: list(sh)
Out[16]: ['22.0', '2016-01-16', '00:16:18']

Upvotes: 0

mementum
mementum

Reputation: 3203

itertools can be of help here

import itertools

s = '22.0;2016-01-16 00:16:18'

sp = itertools.chain(*([x] if not i else x.split(' ')
                       for i, x in enumerate(s.split(';'))))

print(list(sp))

Output:

['22.0', '2016-01-16', '00:16:18']

With a potentially more elegant or (instead of if) syntax:

sp = itertools.chain(*([x] * (not i) or x.split(' ') for i, x in enumerate(s.split(';'))))

Upvotes: 0

Nicholas
Nicholas

Reputation: 486

You can split with the regular expression ;|, like so:

import re

x = '22.0;2016-01-16 00:16:18'
print re.split(';| ', x)

This prints ['22.0', '2016-01-16', '00:16:18'].

Upvotes: 4

Nathaniel Ford
Nathaniel Ford

Reputation: 21220

You can use a list comprehension:

>>> x = "22.0;2016-01-16 00:16:18"
>>> x.strip().split(';')
['22.0', '2016-01-16 00:16:18']
>>> y = [word.split(' ') for word in x.split(';')]
>>> y
[['22.0'], ['2016-01-16', '00:16:18']]
>>> [item for sublist in y for item in sublist]
['22.0', '2016-01-16', '00:16:18']

This is the equivalent of splitting your initial input, then splitting each result on your second delimiter. Finally, you 'flatten' the list.

As a one-liner, this is what you're looking for:

[item for sublist in [word.split(' ') for word in x.strip().split(';')] for item in sublist]

Upvotes: 0

Related Questions