2016-06-15 13 views
0

これはStackOverflowの最初の投稿ですので、あまりにも曖昧であれば謝ります。 基本的には、プログラムから出力されるファイルがたくさんあり、すべてのデータを収集するプロセスを自動化しようとしています。検索、検索、印刷、名前変更 - PythonV2.7.5 String Extractor

私が欲しいのは、印刷された書式で返される文字列の値です。私はそれを達成することができました。しかし、新しく印刷されたファイルの列を作成して、その文字列がどのファイルから来たのかを教えてもらいたい。

私は約6時間しかpythonを使用していないので、どんな助けも非常に高く評価されるでしょう!

ファイルの内容の抜粋:ここ

GROWTH DIRECTION =   0 0 1 
    SLICE SHIFT 5 =    0.00 ANGSTROMS 
    LATTICE ENERGY =   -21.40 KCAL/MOL 
    SLICE ENERGY =    -21.40 KCAL/MOL 
    ATTACHMENT ENERGY =   0.00 KCAL/MOL 
    SURFACE ENERGY =    0.00 
    ------------------------------------------- 

は、私がこれまで持っているものです。

# This script is to be used to pull out lines from strings. 
import re # Standard Regular expression module 


lattE = open("TestFile.txt", "r") # opens the assigned file 
lattEW = open("Lattice_Energies2.txt", "w") # Writes a new document to include all the lines that use LATTICE 


for line in lattE: # looks through every line in the file 
    if re.match("(.*)(L)ATTICE(.*)", line): #searches the lines for LATTICE 
     print >>lattEW, line, # Prints the lines 

電流出力:

LATTICE ENERGY =   -21.40 KCAL/MOL 
    LATTICE ENERGY =   -21.40 KCAL/MOL 
+0

あなたの例では、 "TestFile.txt"という文字列を印刷することもできますか?または、TestFile.txtに既に複数のファイルからの出力が含まれていますか? – user1308345

+0

Pythonのバージョンを追加してください... – wind85

+0

また、ファイルコンテンツのスニペットを追加するといいでしょう。 – wind85

答えて

0

あなたはファイルオブジェクトのname属性を使用することができます。

for line in lattE: 
    if re.match("text", line): 
     print lattE.name, line, 
+0

だから lattE = open( "*。txt"、 "r")?? – AlexAMP

+0

'open(...) 'を書くことで、この関数の戻り値は[ファイルライクなオブジェクト](https://docs.python.org/3/glossary.html#term-file- object)を作成します。基底のファイルの名前(あなたが開いた関数と呼んだ文字列)を含む 'name'属性を持ちます。 –

+0

AHHH !!それは完璧に働いた! これで、ファイルの数が異なるようにするにはどうすればよいのですか?test_file1.txt、test_file2.txtなどがあるとしましょう。 – AlexAMP

0

だから私は少しさらに検索し、これは本当に便利なポスト[https://askubuntu.com/questions/352198/reading-all-files-from-a-directory][1]

を発見し、私の既存のコードでそれを組み合わせてみました...それはに印刷されていないようですしてきました出力ファイル:|

import sys 
import glob 
import errno 
import re 

path = '\*.sum' 
files = glob.glob(path) 
lattEW = open("Lattice_En.txt", "a") 
for name in files: # 'file' is a builtin type, 'name' is a less-ambiguous variable name. 
    try: 
     with open(name) as f: # No need to specify 'r': this is the default. 
      sys.stdout.write(f.read()) 
     for line in name: # looks through every line in the file 
       if re.match("(.*)(L)ATTICE(.*)", line): #searches the lines for LATTICE 
        print >>lattEW, name, line, 
    except IOError as exc: 
     if exc.errno != errno.EISDIR: # Do not fail if a directory is found, just ignore it. 
      raise # Propagate other kinds of IOError 
0

代替方法...実際に働いた...ありがとう@ガボールFekete。

import fnmatch #Imports the synx for itteratively searching through Files 
import os 
import re #regex Expressions 
for file in os.listdir('/.'): #Goes through Files in Directory 
    if fnmatch.fnmatch(file, '*.sum'): # Selects files with .sum extension 
     lattE = open(file,"r") #opens said files 
     lattEW = open("Latt_En.txt", "a") #Amends Summary file 
     for line in lattE: # Goes through file 
      if re.match("(.*)(L)ATTICE(.*)", line): #Searches for term LATTICE 
       print >>lattEW, lattE.name, line, #Prints to LattEW file , name and line 
関連する問題