よろしくお願いいたします.
文字列を空白で分割してリストにしたいのですが,
ダブルクオートやシングルクオートで囲まれている場合は分割しないようにしたいです.
つまり,
python
1str = 'ab c de "f g hi" "jk l m'n"'
という文字列を
python
1["ab", "c", "de", "f g hi", "jk l m'n"]
のように分けたいのです.
なにか良い解決方法や,このような機能を持つパッケージなどご存知でしたら
教えていただけないでしょうか?
よろしくお願いいたします.
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答4件
0
ベストアンサー
正規表現使うならこんな感じですかね
python
1import re 2 3string = 'ab c de "f g hi" "jk l m\'n"' 4re_quotation = re.compile('".*?"|\'.*?\'') 5split_list = re_quotation.sub('', string).split() + re_quotation.findall(string)
正規表現でまず、最短一致(.*?)でコーテーション(single or double)で囲まれた文字列を作る。
subをつかって、コーテーションに囲まれている文字を削除して、スペースでスプリット。
findallをつかって、さっきとは逆にマッチした部分を取る。(findallはリストとしてかえってきます。)
そして最後に足せば、完成。
投稿2015/11/13 12:26
総合スコア32
0
お題をこなすだけならこれでよかったですね。
バグもなくて堅牢。やっぱ酒飲んでコーディングすると駄目ですね。
python
1import csv 2 3testcase = r'''ab c de "f g hi" "jk l m'n"''' 4 5print('input: ' + str(testcase)) 6 7rows = csv.reader([testcase], delimiter=' ', quotechar='"') 8for token in list(rows)[0]: 9 print(' ' + token) 10 pass 11pass
実行結果
$ python csv_split.py
input: ab c de "f g hi" "jk l m'n"
ab
c
de
f g hi
jk l m'n
投稿2015/11/13 17:52

退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
こうかなあ。バグってたらごめんなさい。
python
1def myspliter(text): 2 list = [] 3 token = [] 4 quote1 = False 5 quote2 = False 6 i = 0 7 8 c1 = text[i:i+1] 9 i += 1 10 c2 = text[i:i+1] 11 i += 1 12 13 while c2: 14 if c1 == '\\' and (c2 == '\'' or c2 == '\"'): 15 token.append(c1) 16 token.append(c2) 17 18 c1 = text[i:i+1] 19 i += 1 20 c2 = text[i:i+1] 21 i += 1 22 continue 23 24 if (not quote2) and c1 == '\'': quote1 = not quote1 25 if (not quote1) and c1 == '\"': quote2 = not quote2 26 27 if (not (quote1 or quote2)) and c1 == ' ': 28 c1 = c2 29 c2 = text[i:i+1] 30 i += 1 31 list.append(''.join(token)) 32 token = [] 33 continue 34 35 token.append(c1) 36 c1 = c2 37 c2 = text[i:i+1] 38 i += 1 39 pass 40 41 token.append(c1) 42 list.append(''.join(token)) 43 44 return list 45 46testcases = [ 47 r'''ab c de "f g hi" "jk l m'n"''', 48 r'''hoge hoge "hage hage" \"ho e\"e h\"o \"e\'e "do\"u'ble q" 'si\'ng"le q' "re n"zokucase ren"zo ku"case''', 49 ] 50 51for testcase in testcases: 52 print('input: ' + str(testcase)) 53 for token in myspliter(testcase): 54 print(' ' + token) 55 pass 56 pass
実行結果
$ python split_space.py
input: ab c de "f g hi" "jk l m'n"
ab
c
de
"f g hi"
"jk l m'n"
input: hoge hoge "hage hage" "ho e"e h"o "e'e "do"u'ble q" 'si'ng"le q' "re n"zokucase ren"zo ku"case
hoge
hoge
"hage hage"
"ho
e"e
h"o
"e'e
"do"u'ble q"
'si'ng"le q'
"re n"zokucase
ren"zo ku"case
投稿2015/11/13 17:34

退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
javascript
1'ab c de "f g hi" "jk l m\'n"' 2.split('"') 3.reduce(function(arr, elm, i){ 4 if ( i % 2 == 0 ) { 5 Array.prototype.push.apply(arr,elm.split(' ')); 6 }else{ 7 arr.push(elm); 8 } 9 return arr; 10},[]) 11.filter(function(elm){return elm});
どうでしょうか。
#####訂正
ごめんなさい。pythonで書き直しました。
python
1[ x for x in reduce(lambda arr, (i, x): arr + x.split(' ') if i % 2 == 0 else arr + [x], enumerate( 'ab c de "f g hi" "jk l m\'n"'.split('"') ), []) if x ]
投稿2015/11/13 10:51
編集2015/11/13 11:25総合スコア37413
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/13 12:59