2012-01-05 74 views
-2

我想提取文本文件的某些部分。我的輸入文件:如何從文件中提取某些文本?

-- num cell port function safe [ccell disval rslt] 
    "17 (BC_1, CLK, input, X)," & 
    "16 (BC_1, OC_NEG, input, X), " &-- Merged input/ 
    " 8 (BC_1, D(8), input, X)," & -- cell 16 @ 1 -> Hi-Z 
    " 7 (BC_1, Q(1), output3, X, 16, 1, Z)," & 
    " 0 (BC_1, Q(8), output3, X, 16, 1, Z)"; 

,我需要的輸出是這樣:

num cell port function safe ccell 
    17 BC_1 CLK input X 
    16 BC_1 OC_NEG input X 
    16 BC_1 * control 1 
    8 BC_1 D8 input X 
    7 BC_1 Q1 output3 X 16 1 
    0 BC_1 Q8 output3 X 16 1 

到目前爲止,我嘗試下面的代碼,但它給了指數的錯誤。請建議。

import re 
lines=open("input.txt",'r').readlines() 

for line in lines: 
    a=re.findall(r'\w+',line) 
    print re.findall(r'\w+',line) 
    print a[0],a[1],a[2],a[3],a[4],a[5],a[6] 

我使用的Python 2.6.6和錯誤如下:

['num', 'cell', 'port', 'function', 'safe', 'ccell', 'disval', 'rslt'] 
num cell port function safe ccell disval 
['17', 'BC_1', 'CLK', 'input', 'X'] 
17 BC_1 CLK input X 
Traceback (most recent call last): 
    File "C:\Users\ctee1\Desktop\pyparsing\outputparser.py", line 39, in <module> 
    print a[0],a[1],a[2],a[3],a[4],a[5],a[6] 
IndexError: list index out of range 
+0

什麼語言是這個呢?我們能否得到確切的異常信息的副本? – Dessus 2012-01-05 03:54:15

+0

可能是python? – 2012-01-05 04:00:38

回答

0
sed -r 's/(disval|rslt)//; s/(.)--.*/\1/; s/[^[:alnum:]_]//; s/ +$//' 
+0

是的,我在Python 2.6.6上運行,錯誤如下: – maximus 2012-01-05 05:00:34

相關問題