看板 Python 關於我們 聯絡資訊
大家好 最近再做分割字串的學習,當字串內容只有單(')或雙引號(")時 程式碼我知道如何撰寫(作法如下) #只處裡單引號 string = " \"%s\" " %(user_input) //user_input: 使用者所輸入的字串 #只處理雙引號 (string = " \'%s\' " %(user_input) ) strings = shlex.split(string) 但以上程式碼都無法分割同時有單/雙引號的字串, 出現的error訊息都為 No closing quotation 想請教各位有什麼方法可解決? 謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 150.117.56.52 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1461247072.A.2CA.html
kajm: 忽略了什麼參數設定呢QQ 04/22 01:23
kajm: 回錯文 sorry 04/22 01:23
octantis: 不懂你"無法分割同時有單/雙引號的字串"是什麼意思? 04/22 02:27
octantis: 如果是'test, "test1", \'test2\''是可以分的啊 04/22 02:28
aweimeow: 你用的是 py2 嗎? user_input 是用 input() 接進來的嗎 04/22 09:19
CaptainH: 沒事用shlex做什麼… 04/22 14:31
ENEP: 用""" http://stackoverflow.com/questions/7487145/ 04/22 17:58