Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of things you can do:</p> <ul> <li>Use functions (they will let you break up units of work into easily-understandable pieces)</li> <li>Use <a href="http://docs.python.org/2/library/re.html" rel="nofollow">regular expressions</a> (they will greatly simplify your pattern matching)</li> <li>Use <a href="http://docs.python.org/2/tutorial/classes.html#generators" rel="nofollow">generators</a> (they will help you do everything in a single pass)</li> </ul> <p>Here is some slightly re-worked code with comments to get you started:</p> <pre><code>import collections import re DATA_LINE = re.compile("(\d{8})=(.*)") DATE = re.compile(""" (?P&lt;month&gt;\w+) # Match one or more alpha-numeric characters # (store match as "month") \s+ # followed by one or more whitespace characters (?P&lt;day&gt;\d{1,2}) # followed by one or two numbers # (store match as "day") \s+ # followed by one or more whitespace characters (?P&lt;time&gt;\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&lt;year&gt;\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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload