0
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

How can i have first two elements of each line as key and value. for line 1 key =101103, value = Baldwin, C

  • Does this answer your question? [Split string on whitespace in Python](https://stackoverflow.com/questions/8113782/split-string-on-whitespace-in-python) – ik1ne Oct 30 '19 at 02:10

1 Answers1

0

You could try using re.findall here with the following pattern:

(\d+)\s+(\w+, [A-Z])\s+(\w+)

Sample script:

inp = """
    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"""

parts = re.findall(r'(\d+)\s+(\w+, [A-Z]).*', inp)
d = {}
for elem in parts:
    try:
        d[elem[0]].append(elem[1])
    except KeyError:
        d[elem[0]] = [elem[1]]

print(d)

This prints:

{'10183': ['Chapman, O'], '10172': ['Forbes, I'], '10175': ['Erickson, D'],
 '11399': ['Cordova, I'], '11658': ['Kelly, P'], '11461': ['Wright, U'],
 '11714': ['Morton, A'], '10115': ['Wyatt, X'], '11788': ['Fuller, E'],
 '10103': ['Baldwin, C']}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360