-1

I am looking for help on string manipulation in Python 3.

Input String

s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"

Desired Output

s = "ID,FIRST_NM,LAST_NM,FILLER1"

Basically, the objective is to remove anything between space and comma at all occurrences in the input string.

Any help is much appreciated

deadshot
  • 8,881
  • 4
  • 20
  • 39
Dev
  • 63
  • 1
  • 7
  • Does this answer your question? [Replace a string located between](https://stackoverflow.com/questions/11096720/replace-a-string-located-between) – RichieV Sep 06 '20 at 04:19
  • ``",".join(x.split()[0] for x in s.split(","))`` ? – sushanth Sep 06 '20 at 04:26

2 Answers2

1

You can use regex

import re

s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
s = ','.join(re.findall('\w+(?= \w+)', s))
print(s)

Output:

ID,FIRST_NM,LAST_NM,FILLER1
deadshot
  • 8,881
  • 4
  • 20
  • 39
1

using simple regex

import re
s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
res = re.sub('\s\w+', '', s)
print(res) 

# output ID,FIRST_NM,LAST_NM,FILLER1
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21