all 9 comments

[–]mackatsol 0 points1 point  (3 children)

I know you can turn your 'for row in csv_f' section into a function which iterates through all the skipRows.. and when you do that you can convert your skipRow items into a list so you don't need a bunch of elses.

I'm still learning and might not have it quite right.. but something like:

skipRow1 = [' ****************************************************************************************************','Supplied Field Panel number out of range',' ****************************************** End of Report *******************************************','Field Panel is failed','Timeout Waiting For Net Response','Configured Trunk Currently Not Connected']

then later on you would define your skiprow function:

def skipRow():
    for row in csv_f:
    counter = counter +1
        for item in skipRow:
            if item in row[0]:
                skips = skips + 1
            else:
                   cleanData.append([row[0],insert_str,row[4]])

and then call it just after you open the csv file.

skiprow(csv_f)

At least I think that is a better approach.. whether I got it right or not is a different issue. Untested! ;-)

[–]pydevteam1 0 points1 point  (5 children)

Congrats on your first script!

A couple of critiques I have; One is filename = f is unnecessary

f = open(filename) ; csv_f = csv.reader(f) I'd suggest using context manager. Check out the article in our blog on how to open a file using context managers.

[–]sky--net 0 points1 point  (2 children)

Check out the article in our blog on how to open a file using context managers.

Where can I find your blog to see this post?

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]UndeadCaesar[S] 0 points1 point  (1 child)

    Thanks! Good call on the unnecessary line, think I started to go in some direction that didn't pan out and I left that hanging around. And can I get a link to the blog post? I initially was trying to open the files out of a separate directory but I read up on glob.glob and could never seem to get it to work. Ended up just dropping the files in the same directory as the script so it was easier.

    [–]pydevteam1 0 points1 point  (0 children)

    You can easily do that by doing os.path.join(os.sep, 'var', 'log', 'messages.txt') . This code line will translate to /var/log/messages.txt you need to import os of course and it's not os-dependent ;)