2016-09-15 9 views
0

私はかなり新しいです。私は、他の不要なブロックと一緒に、次のフォーマットで多くのデータブロックを含むテキストファイルを持っています。Pythonを使ってテキストファイルの各行を処理する

NOT REQUIRED :: 123 
    Connected Part-1:: A ~$ 
    Connected Part-3:: B ~$   
    Connector Location:: 100 200 300 ~$ 
    NOT REQUIRED :: 456 
    Connected Part-2:: C ~$ 

私は情報(A、B、C、100 200 300)各プロパティに対応する(接続されたパート1、コネクタの位置)を抽出し、後でそれを使用するリストとして保存することを望みます。私はファイルを読み込み、行を消去し、リストとして保存するコードを次のように準備しました。ここ

import fileinput 
    with open('C:/Users/file.txt') as f: 
    content = f.readlines() 
    for line in content: 
      if 'Connected Part-1' in line or 'Connected Part-3' in line: 
        if 'Connected Part-1' in line: 
          connected_part_1 = [s.strip(' \n ~ $ Connected Part -1 ::') for s in content]        
          print ('PART_1:',connected_part_1)         
        if 'Connected Part-3' in line: 
          connected_part_3 = [s.strip(' \n ~ $ Connected Part -3 ::') for s in content]        
          print ('PART_3:',connected_part_3)        
      if 'Connector Location' in line:       
        # removing unwanted characters and converting into the list 
        content_clean_1 = [s.strip('\n ~ $ Connector Location::') for s in content] 
        #converting a single string item in list to a string 
        s = " ".join(content_clean_1) 
        # splitting the string and converting into a list 
        weld_location= s.split(" ") 
        print ('POSITION',weld_location)  

は、このプログラムの出力から出力

PART_1: ['A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t'] 
    POSITION ['d', 'Part-1::', 'A', '\t\tConnector', 'Location::', '100.00', '200.00', '300.00', '\t\tConnected', 'Part-3::', 'C~\t'] 
    PART_3: ['1:: A', '\t\tConnector Location:: 100.00 200.00 300.00', '\t\tConnected Part-3:: C~\t'] 

で、「コンテンツ」は、ファイル内のすべての文字からなる文字列であるので、私は、それを締結することができるプログラムが読んでいません個々の行代わりに、すべてのテキストを1つの文字列とみなしています。誰もこの場合に助けてくれますか?

私は次の出力を期待しています:

PART_1: ['A'] 
    PART_3: ['C'] 
    POSITION: ['100.00', '200.00','300.00'] 

(注意)私は単一のデータ行を含む個々のファイルを使用していた場合、それが正常に動作します。このような長い質問に申し訳ありません

+0

「接続されたパート1」または「接続されたパート3」の行をチェックするのはなぜですか: '次に入れ子にされたifで再度チェックしますか?どうして 'if 'Connected Part-1'の行に' 'elif Connected Part-3 'が' 'if/or if? –

答えて

0

私はそれを明確にしようとし、regexなしでそれを行う方法を示します。まず、提示したコードとの最大の問題は、string.strip機能を使用する場合、コンテンツ全体のリストが読み込まれていることである:

connected_part_1 = [s.strip(' \n ~ $ Connected Part -1 ::') for s in content] 

コンテンツはファイル全体の行で、私はあなたが単にような何かをしたいと思います

connected_part_1 = [line.strip(' \n ~ $ Connected Part -1 ::')] 

どのようにファイルを解析するために少し主観的であるが、入力として計上ファイル形式を考えると、私はこのようにそれを行うだろう:

templatestr = "{}: {}" 

with open('inputreadlines.txt') as f: 
    content = f.readlines() 
    for line in content: 
     label, value = line.split('::') 
     ltokens = label.split() 
     if ltokens[0] == 'Connected': 
      print(templatestr.format(
       ltokens[-1], #The last word on the label 
       value.split()[:-1])) #the split value without the last word '~$' 
     elif ltokens[0] == 'Connector': 
      print(value.split()[:-1]) #the split value without the last word '~$' 
     else: #NOT REQUIRED 
      pass 

することができますこの例のように最後のトークンを削除する代わりに、string.strip関数を使用して面白い文字 '〜$'を削除してください。

+0

:ありがとうございます。しかし、テキストファイルからのキーワード(Connected Part-1、Connector Location)を比較する条件が満たされていない場合は、条件が満たされていません。プログラムは'else 'を実行しています。明らかに、上記のコードでは、テキストファイルに記載されている正確なキーワードを使用しています。 – makino

+0

しかし、私はヒントを得た、さらに動作します。 – makino

+0

@ rllありがとうございました。 – makino

関連する問題