包含標點英文語句拆分成列表 題(笨方法解決)

2021-08-31 21:34:36 字數 868 閱讀 1645

題目:

分析:python提供split()函式將字串拆分成列表,但是此題中有標點,而且標點2側無空格

方法1:用了最笨的方法,先將符號踢除,再轉

if __name__ == '__main__':

print('原始字串%s'%(s))

if ',' in s:

s1 = s.replace(","," ")

print(('去除符號,後的字串%s'%(s1)))

if '.' in s1:

s2 = s1.replace("."," ")

print('去除符號.後的字串%s'%(s2))

if '?' in s2:

s3 = s2.replace("?"," ")

print('去除符號?後的字串%s'%(s2))

result = s3.strip().split()

print('最後列表為%r'%(result))

結果:

方法:另外 一種方法:先用標點符號分割,再用空格連線

if __name__ == '__main__':

print(s.split(','))

s1 = ' '.join(s.split(','))

print(s1)

s2 = ' '.join(s1.split('.'))

print(s2)

s3 = ' '.join(s2.split('?'))

print(s3)

結果:

pandas date拆分成單獨的列

原資料data date time date time other columns 2012 01 0100 00 00.2012 01 0100 15 00.2014 12 3123 30 00.2014 12 3123 45 00.將 年月日時分秒 全部拆分為單獨的列 將data date ti...

dataframe一列拆分成多列 split

假設某一列資料報含多個資訊或乙個字串 id attrs a 1,2,5,3 b 3,1,2,5 c 1,2,0,3 d 1,7,5,3 e 2,1,6,8 我們想把他拆分成多列,做法如下 首先進行拆分 data df data df attrs str.split expand true 然後用pd...

T SQL行合併成列與列拆分成行

原文 t sql行合併成列與列拆分成行 本文出處 感覺最近sql也沒少寫,突然有一點生疏了,對於用的不是太頻繁的一些操作,時間一久就容易生。多行的某乙個列合併成乙個列 另外是乙個相反的操作,借助上面合併之後的結果,將乙個多個值的字串列拆分開來,轉換為多行,用到的字串拆分函式,比較常見 create ...