2017-10-11 4 views
0

.txtファイルをループし、そのファイルの最初の行の日付(たとえば、April 1、1993)を使用します。Python Regex:ディレクトリ内の各ファイルの最初の行をループします。

このコードは動作しますが、ファイル全体だけではなく、最初の行(注:コードImは以下を示すだけで、日付マッチングループよりも多くを示してい):経由と一致する以下のスクリプトが更新され、それが動作

:のみファイルの1行目に日付を一致させるためには

articles = glob.glob("*.txt") 
y = 1 

for f in articles: 
    with open(f, "r") as content: 
     wordcount = "x" 
     lines = content.readlines() 
     for line in lines : 
      if line[0:7] == "LENGTH:": 
       lineclean = re.sub('[#%&\<>*?:/{}[email protected]+|=]', '', line) 
       wordcount = lineclean[7:13] 
       if wordcount[5] == "w": 
        wordcount = wordcount[0:4] 
       elif wordcount[4] == "w": 
        wordcount = wordcount[0:3] 
       elif wordcount[3] == "w": 
        wordcount = wordcount[0:2] 
       elif wordcount[2] == "w": 
        wordcount = wordcount[0:1] 
    with open(f, "r") as content: 
     first_line = next(content) 
     try: 
      import re 
      match = re.search('(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}', first_line).group() 
     except: 
      pass   
     from dateutil import parser 
     parsed_pubdate = parser.parse(match).strftime('%Y-%m-%d')     
    try: 
     if wordcount != "x": 
      move(f, "{parsed_pubdate}_{wordcount}_{source}.txt".format(**locals())) 
     else: 
      pass 
    except OSError: 
     pass 
    y += 1 
    content.close() 

、私は^\sflags=re.MULTILINEを追加するので、私は得る:

match = re.search('^\s(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)? 
|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)? 
|Dec(ember)?)\s+\d{1,2},\s+\d{4}', line, flags=re.MULTILINE).group() 

ただし、プログラムは1つの日付(フォルダ内の最後のファイルの日付)のみを使用し、すべてのファイルにその日付を使用します(したがって、元の.txtファイルの日付は異なります)。

私はこのループがの一部ですが、私の問題は、唯一の正規表現日付マッチングループに適用される全体のステップをuncluded。あなたの助けを前もってありがとう!

+0

":"を削除しているため、6で単語カウントを開始する必要がありますか? 最初の行だけをチェックしたいのであれば、なぜfirs_line = content.readlines()[0]ですか? –

+0

@AlfredoMiranda:私は試み... '後最初のものを除くすべてを破棄するようにすべての行を読み取り避けるため' –

+0

をfirst_line =次(コンテンツ)をprefereなる 'first_line = content.readlinesは()[0]'それが与えますRegexの 'first line specifer'とまったく同じ問題です。つまり、1つの.txtファイルの日付のみを使用し、それをすべてのファイルに適用します。 Re:単語数。それは現在のスクリプトでうまく動作します。 – Rens

答えて

0
articles = glob.glob("*.txt") 
y = 1 

for f in articles: 
    with open(f, "r") as content: 
     wordcount = "x" 
     lines = content.readlines() 
     for line in lines : 
      if line[0:7] == "LENGTH:": 
       lineclean = re.sub('[#%&\<>*?:/{}[email protected]+|=]', '', line) 
       wordcount = lineclean[7:13] 
       if wordcount[5] == "w": 
        wordcount = wordcount[0:4] 
       elif wordcount[4] == "w": 
        wordcount = wordcount[0:3] 
       elif wordcount[3] == "w": 
        wordcount = wordcount[0:2] 
       elif wordcount[2] == "w": 
        wordcount = wordcount[0:1] 
    with open(f, "r") as content: 
     first_line = next(content) 
     try: 
      import re 
      match = re.search('(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2},\s+\d{4}', first_line).group() 
     except: 
      pass   
     from dateutil import parser 
     parsed_pubdate = parser.parse(match).strftime('%Y-%m-%d')     
    try: 
     if wordcount != "x": 
      move(f, "{parsed_pubdate}_{wordcount}_{source}.txt".format(**locals())) 
     else: 
      pass 
    except OSError: 
     pass 
    y += 1 
    content.close() 
関連する問題