跳到主要內容

[Python] Parse 左右括號內的變數宣告

以前做研究時,常用正規擷取兩個括號之間的 Attributes,用一個例子解釋如下。

假設今天有一個檔案內容,描述一筆資料結構:

Person Tom{
                      name    Tom,
                      age        20,
                      gender  male
                   };

如果想擷取出:

1 : Tom
2 : 20
3 : male

用 Python 的 regex 具體作法,我分成兩步驟:
  1. 先將檔案內容從 Person 開始到結尾分號,存入一個 string 叫做 full_line,但注意不要存 \n 換行字元到 full_line 內。
  2. 用正規將 full_line 內大括號內的字元都擷取出來,也就是擷取:
    "name Tom, age 20, gender male"
  3. 再利用正規,將第 2 步驟擷取出的字串,用 re.findall 逐一擷取 attributes 的 key 值。
 Code 如下:

full_line = ""    
with open(fileName, 'r') as f:
        for line in f.readlines():
            line = line.strip()
            if not line:    continue   
            // eliminate lines filled with white spaces
            
            print line
            
            full_line += line 
            if full_line.find(';') == -1:
                full_line += " "
                continue


通常檔案內的資料會分為多行並縮排以方便閱讀,因此利用偵測分號,將開頭到結尾分號的內容存入 full_line,拿掉 \n 符號。

接著參考這個討論串的作法,將左右大括號內的內容取出:
     
            pattern = re.compile(r"""
                                   (\w+)\s+(\w+) 
                                   \s*\{
                                         ([^)]+)
                                   \}\s*;
                                 """, re.VERBOSE|re.MULTILINE)
            
            match = pattern.search(full_line)
            if match is not None:
                content = match.group(3)
             
                pat = "[^,]+\s(\w+)\s*,?"
                var_list = re.findall(pat, content)

                for i in range(len(var_list)):
                    print i, ":", var_list[i]



執行結果
0 : Tom
1 : 20
2 : male

找到我們要的 Person 資料結構之後,先將括號內的資料存在 content 內,re.findall 會返回一個 list ,將符合比對項目的所有的 attributes 存入此 list 內。


留言

這個網誌中的熱門文章

[Linux] Elementary OS 字體調校

用 gesetting 取得 elementary os 的等寬字體(這也是終端機默認字體): gsettings get org.gnome.desktop.interface monospace-font-name 會顯示目前字型跟字體大小: Roboto Mono 10 設定字體大小: gsettings set org.gnome.desktop.interface monospace-font-name 'Roboto Mono 12' 可以微調 text-scaling-factor: gsettings set org.gnome.desktop.interface text-scaling-factor <value>

[Vim] 我的 .vimrc

我用過的文字編輯器有 sublime text/atom/notepad++/gedit 等等。 後來我在 window 和 Linux 寫程式還是比較慣用 vim。 以下整理我常用的 .vimrc:  set nu "show line number. set rnu "related line number. nmap ; : "replace ';' with ':' in normal mode. set ai "auto indent set mouse=a "activate mouse set expandtab "replace tab with space. set tabstop=4 "replace tab with 4 spaces. set bg=dark "background set to dark. color desert "color scheme. syntax on "enable syntax highlight. " auto complete: inoremap ( ()<Esc>i inoremap ' ''<Esc>i inoremap " ""<Esc>i inoremap [ []<Esc>i inoremap { {}<Esc>i set cursorline " active cursorline 到此 vim 搭配 Ctrl + N 當自動補全,已經很好用了,之後有好用設定會再補上來。

[Python] print 同時輸出到 file 和 console

在 Python 撰寫程式時,我們會希望螢幕 stdout 輸出可以同時記錄到 log 檔案裡。 但是螢幕輸出可能含有 ASCII escape codes 的顏色資訊,輸出的 log 檔案會有類似 ^[[01;32m 這種字樣出現。 我採用比較簡單的解法: 先將 print 函式輸出的訊息,同時導向到螢幕,同時儲存在指定的 log.txt 檔案中。 再用 sed 指令,將 log.txt 內的 ASCII escape code 清除。 方法如下: import sys class PrintLog(object): def __init__(self): self.console = sys.stdout self.log_file = open("log.txt", "w") def write(self, msg): self.console.write(msg) self.log_file.write(msg) def flush(self): pass def main(): original_stdout = sys.stdout sys.stdout = PrintLog() print " This is a testing message." sys.stdout = original_stdout if(__name__ == "__main__"): main() 也就是將 sys.stdout 指向自定義的 PrintLog class,讓 PrintLog 來處理輸出文字,用完 PrintLog 後再把 sys.stdout 導向回原本的 stdout。 接著使用 sed 指令刪除 log.txt 的 ASCII escape code: sed -i 's/\x1b[^m]*m//g' ./log.txt 上面的正規,\x 後面用來接一個 16 進位 ASCII 編碼,其中 1b 代表的是 ESC 退出鍵。 到此即可獲得沒有顏色編碼的 log.tx...