Pythonã§ã³ãã³ãè¡ã®ãããªæååã解æãã¦ãªã¹ãã«æ ¼ç´ããä¸ã§ã®ã¡ã¢(å¾å)
ãPythonでコマンド行のような文字列を解析してリストに格納する上でのメモ(前半)ãã®ç¶ãã
ããã«ã¯ã©ã¼ããããé¨åã¨ããã§ãªãé¨åã¨ããã¾ãå¦çãã
è²ã 試ããçµæãããã«ã¯ã©ã¼ãæåãåºåãã¨ãã¦å ã®æååãåå²ãã¦ãããã¨ã§ããã«ã¯ã©ã¼ããããé¨åã1ã¤ã®ã¾ã¨ã¾ãã¨ãã¦å¾ããã¨ãã§ãããã¨ãããã¨ãåãã£ãã
(è²ã 試ãã¦ããã¨ãã) >>> 'aaa bbb ccc "ddd eee fff" ggg hhh iii'.split('"') ['aaa bbb ccc ', 'ddd eee fff', ' ggg hhh iii'] >>> 'aaa bbb ccc "ddd eee fff" ggg "hhh iii'.split('"') ['aaa bbb ccc ', 'ddd eee fff', ' ggg ', 'hhh iii'] >>> 'aaa bbb ccc "ddd eee fff" ggg "hhh iii" jjj kkk'.split('"') ['aaa bbb ccc ', 'ddd eee fff', ' ggg ', 'hhh iii', ' jjj kkk']
split()ã§å¾ããããªã¹ãã®å·¦ããå¶æ°çªç®(ãªã¹ãå
ã®çªå·ã¯0ãããªã®ã§å¥æ°)ã®è¦ç´ ãããã«ã¯ã©ã¼ãå
ã®å
容ã¨ãªããæ®ããããã«ã¯ã©ã¼ããå«ã¾ãªãé¨åã§ãã®ã¾ã¾ã¹ãã¼ã¹æåãsplit()ã®å¼æ°ã«ãã¦åå²ã§ããã
ãã ããããã«ã¯ã©ã¼ããããé¨åãã³ãã³ãè¡ãæ§æãã1ã¤ã®è¦ç´ ã®å
¨ä½ã¨ã¯ãªããªããã¨ããããããã«ã¯ã©ã¼ãã®åå¾1æåãã¹ãã¼ã¹ãã©ããã§å ´ååãããå¿
è¦ããã£ãã
è²ã
試ããçµæã以ä¸ã®ãããªå¦çã§ãã¾ã解æã§ãããã¨ãåãã£ãããã¾ã ããããé¨åãããå¯è½æ§ããã£ã¨ãã¾ãæ¹æ³ãããå¯è½æ§ãããã
[ä»»æ]ãã¡ã¤ã«å: parsecmdlinetest.py
#! /usr/bin/python # -*- encoding: utf-8 -*- # ã·ã§ã«ããå®è¡ããã³ãã³ãè¡ã®ãããªæååã解æãã¦ãªã¹ãã«æ ¼ç´ # ããã«ã¯ã©ã¼ããããé¨åã¯ã¹ãã¼ã¹ãå«ããã¨ãã§ããããã«ãããã® # é£ç¶ããã¹ãã¼ã¹ã¯ç¡è¦ããã1ã¤ã®ã¹ãã¼ã¹ã¨åçã«æ±ã def parse_cmdline(in_str): # åºåãªã¹ã out = [] # å¥æ°/å¶æ°ãã¿ãããã«çªå·ã¨åæã«åã for i, part in enumerate(in_str.split('"')): if i % 2 == 0: # å¶æ°çªã¯ã¯ã©ã¼ãå¤ # ã¹ãã¼ã¹ã§åå²ãã¦ããããã追å # æåãã¹ãã¼ã¹ãªãã¯ã©ã¼ãå ã追å quoted = '' if i >= 2: # æåãã¹ãã¼ã¹ãªãã¯ã©ã¼ãå ã®å 容ããªã¹ãã®ä¸çªä¸ããå¥ãã㦠# å é ã«ãã£ã¤ãã # æå(0çª)ã®è¦ç´ ã¯å¦çããªãã®ã§i >= 2 if part != '' and not part.startswith(' '): quoted = out.pop() # ã¯ã©ã¼ãå¤ã®ã¾ã¨ã¾ããã¹ãã¼ã¹ã§çµãããªãå ´åã«ãã©ã°ãTrueã« # ã¹ãã¼ã¹ã§çµãããªããã©ã°ãFalseã«ãã # æåã®ä»£å ¥ã¯0çªã§è¡ãããã®ã§foræããä¸ã§åæåããå¿ è¦ã¯ãªã cat_quoted_start = not part.endswith(' ') # ã¯ã©ã¼ãå¤ã®æåã®è¦ç´ ãå¦çããã¨ããèå¥ããããã®ãã©ã° first_out = True for item in part.strip().split(' '): # ã¹ãã¼ã¹ãé£ç¶ããå ´åã®å¯¾ç if item != '': # å¥ãããã¯ã©ã¼ãå ã®å 容ã¯æåã ããã£ã¤ãã if first_out == True: first_out = False out.append(quoted + item) else: out.append(item) else: # å¥æ°çªã¯ã¯ã©ã¼ãå # ç´åãã¹ãã¼ã¹ãªãã¯ã©ã¼ãå ãã®ã¾ã¾ # ç´åãã¹ãã¼ã¹ã§ãªããªããã®å 容ã«ç¶ãã¦ã¯ã©ã¼ãå ã追å if cat_quoted_start == True: out.append(out.pop() + part) else: out.append(part) return out str1 = 'aaa bbb ccc "ddd eee fff" ggg "hhh iii "jjj' str2 = 'aaa bbb ccc "ddd eee"fff ggg "h"hh iii' str3 = ' ls -l -a "a b c.txt"' str4 = ' ls -l -a c"d e f.tx"t' print parse_cmdline(str1) print parse_cmdline(str2) print parse_cmdline(str3) print parse_cmdline(str4)
ä¸ã¯å®è¡çµæã
['aaa', 'bbb', 'ccc', 'ddd eee fff', 'ggg', 'hhh iii jjj'] ['aaa', 'bbb', 'ccc', 'ddd eeefff', 'ggg', 'hhh', 'iii'] ['ls', '-l', '-a', 'a b c.txt'] ['ls', '-l', '-a', 'cd e f.txt']
é¢é£:GLib(Cè¨èª/Valaè¨èª)ã§ã¯åçã®ãã¨ãç°¡åã«å®ç¾å¯è½
(2010/1/9)Pythonã¨é¢ä¿ã¯ãªãããGLib 2ã©ã¤ãã©ãªã§ã¯ãã®ç¨éã§ã®ä¾¿å©ãªé¢æ°ãç¨æããã¦ããã以åãVala言語で外部プロセスを実行する(スレッドを使用してGTK+のテキストビューに実行結果を表示・コード例)ããªã©ã§ãç¨ãã¦ããã
Cè¨èªã¯
http://library.gnome.org/devel/glib/stable/glib-Shell-related-Utilities.html#g-shell-parse-argv
Valaè¨èªã¯
http://references.valadoc.org/glib-2.0/GLib.Shell.parse_argv.html
ãåç
§ã
Pythonã§ã¯PyGObjectã®è¨èªãã¤ã³ãã£ã³ã°ã«ãã®é¢æ°ããªããããããã¨åããã¨ãå®ç¾ããããã«æ¬è¨äºã®æ¹æ³ãèãããã¨ã¨ãªã£ãã
(2010/1/15)GLibã®ã½ã¼ã¹ãå
¥æã»å±éãã¦glib/gshell.cãéãã¨ãã®é¢æ°ã®ä¸èº«ãè¦ãããããå¦çã¯çµæ§è¤éã§ãã#ããã\ãã¨ãã£ãæåã®å¦çãªã©ãå«ãã§ããããã ã
shlexã使ç¨ããæ¹æ³(aæ°ã«æè¬)
(2010/1/20)aæ°ã«ããææã«ããåãã£ãæ¹æ³ã¨ãã¦ã以åæ±ã£ãshlexã¢ã¸ã¥ã¼ã«(関連記事)ã®shlex.split()ãç¨ããã¨ç°¡åã«ãã®é¡ã®åå²ãè¡ããã¯ã©ã¼ããªã©ã®å¦çããã¾ãè¡ã£ã¦ãããã
>>> import shlex >>> shlex.split('aaa bbb ccc "ddd eee fff" ggg "hhh iii "jjj') ['aaa', 'bbb', 'ccc', 'ddd eee fff', 'ggg', 'hhh iii jjj'] >>> shlex.split('aaa bbb ccc "ddd eee"fff ggg "h"hh iii') ['aaa', 'bbb', 'ccc', 'ddd eeefff', 'ggg', 'hhh', 'iii'] >>> shlex.split(' ls -l -a "a b c.txt"') ['ls', '-l', '-a', 'a b c.txt'] >>> shlex.split(' ls -l -a c"d e f.tx"t') ['ls', '-l', '-a', 'cd e f.txt']
é¢é£è¨äº:
使ç¨ãããã¼ã¸ã§ã³:
- Python 2.6.4