我想使用python re.split()以逗號將一個句子分割成多個字符串,但我不想將其應用於用逗號分隔的單個單詞,例如:重新分割特殊情況以分割逗號分隔的字符串
例
s = "Yes, alcohol can have a place in a healthy diet."
desired result = ["Yes, alcohol can have a place in a healthy diet."]
另一個例子:
s = "But, of course, excess alcohol is terribly harmful to health in a variety of ways, and even moderatealcohol intake is associated with an increase in the number two cause of premature death: cancer."
desired output = ["But, of course" , "excess alcohol is terribly harmful to health in a variety of ways" , "and even moderatealcohol intake is associated with an increase in the number two cause of premature death: cancer."]
任何指針?請。
你嘗試過這麼遠嗎? – depperm
也許你應該在逗號分割,然後重新組合單個單詞與下一個短語。另外,如果有多個這樣的詞「嘿,嘿,嘿,當然,是......」? –
@depperm,我試過像sep = re.split('(?<!\ d)[,](?!\ d)',string)和其他沒有人似乎是防彈的 –