0

How to split the words in line separated by ;:

10103;Baldwin, C;SFEN
10115;Wyatt, X;SFEN
10172;Forbes, I;SFEN
10175;Erickson, D;SFEN
10183;Chapman, O;SFEN
11399;Cordova, I;SYEN
11461;Wright, U;SYEN
11658;Kelly, P;SYEN
11714;Morton, A;SYEN
11788;Fuller, E;SYEN
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Possible duplicate of [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – Paul Rooney Nov 04 '19 at 22:43

3 Answers3

0

Is this what you're looking for ?

line = "10103;Baldwin, C;SFEN 10115;Wyatt, X;SFEN 10172;Forbes, I;SFEN 10175;Erickson, D;SFEN 10183;Chapman, O;SFEN 11399;Cordova, I;SYEN 11461;Wright, U;SYEN 11658;Kelly, P;SYEN 11714;Morton, A;SYEN 11788;Fuller, E;SYEN"
line.split(";")

Output

['10103',
 'Baldwin, C',
 'SFEN 10115',
 'Wyatt, X',
 'SFEN 10172',
 'Forbes, I',
 'SFEN 10175',
 'Erickson, D',
 'SFEN 10183',
 'Chapman, O',
 'SFEN 11399',
 'Cordova, I',
 'SYEN 11461',
 'Wright, U',
 'SYEN 11658',
 'Kelly, P',
 'SYEN 11714',
 'Morton, A',
 'SYEN 11788',
 'Fuller, E',
 'SYEN']
pka32
  • 5,176
  • 1
  • 17
  • 21
0

one alternative:

"10103;Baldwin, C;SFEN".split(";")

However, I think you want to separate everything (including the commas)so I would do a replace of the ";" first into commas and then doing the split by commas.

0

I suggest using csv for this, although if your input is actually a string then you'll need io.StringIO or just split by newline:

import csv
from io import StringIO

s = """10103;Baldwin, C;SFEN
10115;Wyatt, X;SFEN
10172;Forbes, I;SFEN
10175;Erickson, D;SFEN
10183;Chapman, O;SFEN
11399;Cordova, I;SYEN
11461;Wright, U;SYEN
11658;Kelly, P;SYEN
11714;Morton, A;SYEN
11788;Fuller, E;SYEN"""

reader = csv.reader(s.split('\n'), delimiter=';')
#or
reader = csv.reader(StringIO(s), delimiter=';')
for line in reader:
    print(line)

Output:

['10103', 'Baldwin, C', 'SFEN']
['10115', 'Wyatt, X', 'SFEN']
['10172', 'Forbes, I', 'SFEN']
['10175', 'Erickson, D', 'SFEN']
['10183', 'Chapman, O', 'SFEN']
['11399', 'Cordova, I', 'SYEN']
['11461', 'Wright, U', 'SYEN']
['11658', 'Kelly, P', 'SYEN']
['11714', 'Morton, A', 'SYEN']
['11788', 'Fuller, E', 'SYEN']
Jab
  • 26,853
  • 21
  • 75
  • 114