A
A
Artem Marsh2018-11-15 16:25:32
Python
Artem Marsh, 2018-11-15 16:25:32

How to split 2 strings?

Let's say I have a file in which I have certain data written:
"Xxxxx : Yyyyy" They are separated by a
colon.
How can I do that. What would it all be written in a separate variable, i.e.
First = "Xxxxx"
Second = "Yyyyy"
Is it possible to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sim3x, 2018-11-15
@sim3x

In [1]: f, s = "Xxxxx : Yyyyy".split(' : ')

In [2]: f, s
Out[2]: ('Xxxxx', 'Yyyyy')

S
sash999, 2018-11-16
@sash999

>>> import os
>>> os.system('cat a.txt')
aaa:bbb
0
>>> with open('a.txt') as a:
... a,b = a.readline() .replace('\n', '').split(':')
...
>>> a
'aaa'
>>> b
'bbb'
>>>

V
Vladimir, 2018-11-16
@vintello

import csv

csv.register_dialect('pipes', delimiter=':')

with open('testdata.pipes', 'r') as f:
    reader = csv.reader(f, dialect='pipes')
    for row in reader:
        print row

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question