作者Amulart (購名棟GG)
看板Python
標題[問題] 字串中有單/雙引號
時間Thu Apr 21 21:57:49 2016
大家好 最近再做分割字串的學習,當字串內容只有單(')或雙引號(")時
程式碼我知道如何撰寫(作法如下)
#只處裡單引號
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