Travis
Travis

Reputation: 23

How does this older, open source .py script differ in language syntax from the latest python 3.0 or greater?

I am fresh meat in the coding world and am diving head-first into python as we speak, so I apologize for such a rookie question in advance. But basically, I got assigned this log parsing project for school and found a seemingly nice parsing script/template online which I thought could be great for me to use. However, after looking at some of the syntax, it became apparent that it was written in an older version of python. Since I only started learning python a few weeks ago, I am not confident in understanding what syntax needs to change, if any, and if it is properly constructed like I initially assumed. My main question is, after viewing the script below, what statements, methods, variables, functions, ect., do you think must be (or should be) changed in order for me to get the ball rolling?

I'm running Win10 and using Spyder (from Conda) with python version 3.7. I'd post the website link to this code but I don't know the rules on posting links in questions! However, if you search for 'pythonicways' you'll find it, as well as a walk-through guide for why it was made the way it was by the creator.

import time
from time import strftime

def main():
    log_file_path = r"C:\ios logs\sfbios.log"
    export_file_path = r"C:\ios logs\filtered"

    time_now = str(strftime("%Y-%m-%d %H-%M-%S", time.localtime()))

    file = "\\" + "Parser Output " + time_now + ".txt"
    export_file = export_file_path + file

    regex = '(<property name="(.*?)">(.*?)<\/property>)'

    parseData(log_file_path, export_file, regex, read_line=True)



def parseData(log_file_path, export_file, regex, read_line=True):
    with open(log_file_path, "r") as file:
        match_list = []
        if read_line == True:
            for line in file:
                for match in re.finditer(regex, line, re.S):
                    match_text = match.group()
                    match_list.append(match_text)
                    print match_text
        else:
            data = file.read()
            for match in re.finditer(regex, data, re.S):
                match_text = match.group();
                match_list.append(match_text)
    file.close()

    with open(export_file, "w+") as file:
        file.write("EXPORTED DATA:\n")
        match_list_clean = list(set(match_list))
        for item in xrange(0, len(match_list_clean)):
            print match_list_clean[item]
            file.write(match_list_clean[item] + "\n")
    file.close()

if __name__ == '__main__':
    main()

I'm not expecting anything in particular, per se. I just want to know what changes must be or should be made to this template/script. My overall goal is to open & read a .log file, parse it for several various capture groups using regex, store them all in some variables or some sequence type, and then extract that information onto a new .txt file (preferably in CSV format, or some other tabular delimited format)

Upvotes: 0

Views: 48

Answers (2)

BoarGules
BoarGules

Reputation: 16942

This is Python 2. Use the standard library module lib2to3 to convert it to Python 3. That will be far better than trying to update the code manually, particularly as you aren't proficient in either version of Python.

Upvotes: 0

ryati
ryati

Reputation: 360

You should be able to just change the print lines to get this going.

# print match_text
print(match_text)
#####
#print match_list_clean[item]
print(match_list_clean[item])

Also, try running it first next time. Get used to reading traceback (error messages). You can learn a lot from them.

Upvotes: 1

Related Questions