0

いくつかの Python コードについて助けが必要です。これが可能かどうか教えてください。

コードを実行する前のメモ帳の 4050.log データ ファイルの例を次に示します。

port=4050
SOH=
hostname=BlueVectorEMA
uptime=N/A
SHTSOT=N/A
iCenter=N/A
start=51808152
stop=518083AA
cycles=0.0
tagCnt=23
C1Gen1=0.0.0.0
C1Gen2=0.0.0.0
STX=
00045512=119,59,55,60,50,C,00,N/A
00052450=120,60,44,60,43,C,00,N/A
00042260=113,54,51,59,43,C,00,N/A
00046999=114,56,46,58,42,C,00,N/A
00043166=117,58,46,59,42,C,00,N/A
00052651=113,55,48,58,42,C,00,N/A
00050594=118,59,43,59,43,C,00,N/A
00051774=120,60,44,60,42,C,00,N/A
00056767=119,59,43,60,42,C,00,N/A
00043181=109,51,56,58,45,C,00,N/A
00033333=115,56,55,59,43,C,00,N/A
00031475=115,57,43,58,45,C,00,N/A
00042817=118,59,48,59,50,C,00,N/A
00054245=118,58,45,60,41,C,00,N/A
00043589=118,58,46,60,43,C,00,N/A
00053037=116,57,54,59,48,C,00,N/A
00053636=114,55,45,59,42,C,00,N/A
00045642=116,57,49,59,42,C,00,N/A
00045591=118,59,46,59,45,C,00,N/A
00033114=116,58,46,58,44,C,00,N/A
00042075=114,56,54,58,48,C,00,N/A
00044491=113,55,42,58,43,C,00,N/A
00000587=18,0,999,18,46,T,403/002,0030H/3.0
ETX=
EOT=
timestamp=5180a211
logdir=2013-05

Wed May  1 00:03:13 2013
--------------------------------------------------------------------------------


port=4050
SOH=
hostname=BlueVectorEMA
uptime=N/A
SHTSOT=N/A
iCenter=N/A
start=518083AA
stop=51808602
cycles=0.0
tagCnt=23
C1Gen1=0.0.0.0
C1Gen2=0.0.0.0
STX=
00053636=119,59,44,60,41,C,00,N/A
00043181=107,48,56,59,45,C,00,N/A
00046999=116,57,46,59,42,C,00,N/A
00031475=117,58,43,59,45,C,00,N/A
00053037=115,55,54,60,48,C,00,N/A
00052651=117,58,47,59,42,C,00,N/A
00042075=117,57,54,60,48,C,00,N/A
00050594=118,59,43,59,43,C,00,N/A
00045512=115,55,55,60,50,C,00,N/A
00044491=113,55,42,58,43,C,00,N/A
00033114=115,57,46,58,44,C,00,N/A
00045591=117,58,46,59,46,C,00,N/A
00052450=117,58,44,59,43,C,00,N/A
00042260=114,56,51,58,42,C,00,N/A
00043166=116,58,47,58,42,C,00,N/A
00042817=117,59,48,58,50,C,00,N/A
00056767=119,59,43,60,42,C,00,N/A
00054245=119,59,45,60,41,C,00,N/A
00043589=117,57,46,60,43,C,00,N/A
00051774=118,59,44,59,42,C,00,N/A
00045642=113,55,50,58,42,C,00,N/A
00033333=111,53,55,58,44,C,00,N/A
00000587=7,0,999,7,47,T,402/202,0028H/2.5
ETX=
EOT=
timestamp=5180a469
logdir=2013-05

Wed May  1 00:13:13 2013
--------------------------------------------------------------------------------

次は、それが実行される python コードの例です --

#! /usr/bin/python
fob=open('C:/Users/dit3442/Desktop/4050.log','r')
listme=[n for n in fob.readlines() if not n.startswith('start') and \
    not n.startswith('stop') and \
    not n.startswith('timestamp=') and \
    not n.startswith('hostname') and \
    not n.startswith('00000') and \
    not n.startswith('iCenter') and \
    not n.startswith('tagCnt') and \
    not n.startswith('ETX=') and \
    not n.startswith('EOT=') and \
    not n.startswith('logdir') and \
    not n.startswith('port') and \
    not n.startswith('uptime') and \
    not n.startswith('SHTSOT') and \
    not n.startswith('iCenter') and \
    not n.startswith('start') and \
    not n.startswith('stop') and \
    not n.startswith('cycles') and \
    not n.startswith('tagCnt') and \
    not n.startswith('C1Gen2') and \
    not n.startswith('STX=') and \
    not n.startswith('--------') and \
    not n.startswith('SOH=') and \
    not n.startswith('\n') and \
    not n.startswith('-------') and \
    not n.startswith('C1Gen1')]
fob.close()

fob=open('C:/Users/dit3442/Desktop/4050C.log','w')
for line in listme:
fob.write(line.replace(',', '|').replace('=', '|').replace('Wed ', '').replace('Tue ',
   '').replace('Thu ', '').replace('Fri ', '').
replace('May ', '05/').
replace(' 2013',   '').replace('05/ ', '05/').replace(' ', '/2013 '))
fob.close()

次は、私が考えていることの例です-

00045512|119|59|55|60|50|C|00|N/A
00052450|120|60|44|60|43|C|00|N/A
00042260|113|54|51|59|43|C|00|N/A
00046999|114|56|46|58|42|C|00|N/A
00043166|117|58|46|59|42|C|00|N/A
00052651|113|55|48|58|42|C|00|N/A
00050594|118|59|43|59|43|C|00|N/A
00051774|120|60|44|60|42|C|00|N/A
00056767|119|59|43|60|42|C|00|N/A
00043181|109|51|56|58|45|C|00|N/A
00033333|115|56|55|59|43|C|00|N/A
00031475|115|57|43|58|45|C|00|N/A
00042817|118|59|48|59|50|C|00|N/A
00054245|118|58|45|60|41|C|00|N/A
00043589|118|58|46|60|43|C|00|N/A
00053037|116|57|54|59|48|C|00|N/A
00053636|114|55|45|59|42|C|00|N/A
00045642|116|57|49|59|42|C|00|N/A
00045591|118|59|46|59|45|C|00|N/A
00033114|116|58|46|58|44|C|00|N/A
00042075|114|56|54|58|48|C|00|N/A
00044491|113|55|42|58|43|C|00|N/A
05/1/2013 00:03:13
00053636|119|59|44|60|41|C|00|N/A
00043181|107|48|56|59|45|C|00|N/A
00046999|116|57|46|59|42|C|00|N/A
00031475|117|58|43|59|45|C|00|N/A
00053037|115|55|54|60|48|C|00|N/A
00052651|117|58|47|59|42|C|00|N/A
00042075|117|57|54|60|48|C|00|N/A
00050594|118|59|43|59|43|C|00|N/A
00045512|115|55|55|60|50|C|00|N/A
00044491|113|55|42|58|43|C|00|N/A
00033114|115|57|46|58|44|C|00|N/A
00045591|117|58|46|59|46|C|00|N/A
00052450|117|58|44|59|43|C|00|N/A
00042260|114|56|51|58|42|C|00|N/A
00043166|116|58|47|58|42|C|00|N/A
00042817|117|59|48|58|50|C|00|N/A
00056767|119|59|43|60|42|C|00|N/A
00054245|119|59|45|60|41|C|00|N/A
00043589|117|57|46|60|43|C|00|N/A
00051774|118|59|44|59|42|C|00|N/A
00045642|113|55|50|58|42|C|00|N/A
00033333|111|53|55|58|44|C|00|N/A
05/1/2013 00:13:13

私が必要とする唯一のことは、日付がそれらの上の対応する行と一致するようにすることです。

00052450|120|60|44|60|43|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00042260|113|54|51|59|43|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00046999|114|56|46|58|42|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00043166|117|58|46|59|42|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00052651|113|55|48|58|42|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00050594|118|59|43|59|43|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00051774|120|60|44|60|42|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13
00056767|119|59|43|60|42|C|00|N/A|4050|BlueVectorEMA|05/01/2013 00:03:13

何かアイデアはありますか?

4

2 に答える 2

0

あなたができるいくつかのこと:

  • 関数を使用する (作業単位を簡単に理解できるように分割することができます)
  • 正規表現を使用します (パターン マッチングが大幅に簡素化されます)。
  • ジェネレーターを使用する(1 回のパスですべてを実行するのに役立ちます)

以下は、作業を開始するためのコメントを含む、わずかに修正されたコードです。

import collections
import re

DATA_LINE = re.compile("(\d{8})=(.*)")
DATE = re.compile("""
    (?P<month>\w+)    # Match one or more alpha-numeric characters 
                      # (store match as "month")
    \s+               # followed by one or more whitespace characters
    (?P<day>\d{1,2})  # followed by one or two numbers
                      # (store match as "day")
    \s+               # followed by one or more whitespace characters
    (?P<time>\d{2}:\d{2}:\d{2})
                      # followed by two numbers, a colon, two numbers,
                      # another colon, and two more numbers
                      # (store match as "time")
    \s+               # followed by one or more whitespace characters
    (?P<year>\d{4})   # followed by for numbers
                      # (store match as "year")
    """, re.VERBOSE)

def interesting_lines(logfile_path):
    """Yield lines of interest in tuples of
    ``(processed_line, is_date)``."""

    # First, ensure that we will close the file
    # no matter what happens
    with open(logfile_path) as file_object:
        # Loop over all the lines (files are iterators too)
        for line in file_object:
            data = DATA_LINE.match(line)
            if data:
                # If this line has data, get the matched data
                # (the number before the equals sign and everything after it)
                # pass it on as a list of strings with a flag that marks
                # this lineas a data line
                yield ",".join(data.groups()).split(","), False

            elif DATE.search(line):
                # Otherwise, if the line is the date
                # pass on the re-formatted string and a flag that marks
                # this line as the date line
                yield datetime.strptime(line, "%a %B %d %H:%M:%S %Y") \
                              .strftime("%M/%d/%Y %H:%M:%S"), True


def combine_with_date(lines):
    buffer = collections.deque()
    for line, is_date in lines:
        buffer.append(line)
        if is_date:
            current_date = line
            for line in buffer:
                line.append(current_date)
                yield line
            buffer.clear()

def write_to_file(iterable_of_lines):
    # TODO: Create CSV writer with pipe-delimited dialect
    # and write to file


if __name__ == "__main__":
    file_path = raw_input("Please provide a file path:")
    line_generator = interesting_lines(file_path)
    processed_lines = combine_with_date(line_generator)
    write_to_file(processed_lines)
于 2013-07-09T02:42:52.740 に答える
0

コードを読みやすくするために、ここでいくつかのことを行う必要があります。

これで、ログ ファイルが作成されました。考えてみれば、これは基本的に、日付ごとに 1 つずつ辞書を集めたものです。

まず、ファイルをこれらの各セクションに分割します。

このようなもの:

from datetime import datetime

logFile, temp = [], []
with open("4050.log") as f:
    for line in f:
        if "------------------" in line:
            logFile.append(temp[:])
            temp = []
        else:
            temp.append(line)


logDict = {}
for log in logFile:
    tempDict = {}
    for line in log:
        if "=" in line:
            line = line.split("=")        
            tempDict[line[0]] = line[1].strip()

    logDict[log[-1].strip()] = tempDict.copy()


keysWeDontWant = "port SOH hostname hostname SHTSOT iCenter start stop cycles tagCnt C1Gen1 C1Gen2 STX ETX EOT timestamp logdir uptime".split()

for logDate, log in logDict.items():
    for key, value in log.items():
        if key not in keysWeDontWant and not key.startswith("00000"):
            dateTimeString = datetime.strptime(logDate, "%a %B %d %H:%M:%S %Y").strftime("%M/%d/%Y %H:%M:%S")
            print key + "|" + value.replace(",","|") + "|4050|%s|%s" % (log["hostname"], dateTimeString)

仕事をし、少しきれいです。ただし、それはまだあまり良くありません。適切に行うには、そのログファイルの仕様を知る必要があります。ついでにもう寝ます!!

于 2013-07-08T22:37:56.027 に答える