我正在尋找有關 Python 3 中字符串操作的幫助。輸入字符串s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"所需輸出s = "ID,FIRST_NM,LAST_NM,FILLER1"基本上,目標是刪除輸入字符串中所有出現的空格和逗號之間的任何內容。任何幫助深表感謝
2 回答

收到一只叮咚
TA貢獻1821條經驗 獲得超5個贊
使用簡單的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

繁星點點滴滴
TA貢獻1803條經驗 獲得超3個贊
您可以使用正則表達式
import re
s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
s = ','.join(re.findall('\w+(?= \w+)', s))
print(s)
輸出:
ID,FIRST_NM,LAST_NM,FILLER1
添加回答
舉報
0/150
提交
取消