1

I've been searching stackoverflow, and I can't find anyone comparing strings to log files.Is it possible to match the strings in the first column of csv file to a log file?

when it match: print true in the third column

otherwise: print undetected

for example:

             heading1                        heading2               heading3
5bfa1989e2f6e4a6af9ff62930f462e6b8632212     blablabla                true
56ef50c4b83c17e03400d129de99869d8ab18c94     blablabla              undetected

Assuming my log file has scrambled text like this:

1537763092  0   1   1   1537734291  1537734291  1537734291  8224    93  364544  5bfa1989e2f6e4a6af9ff62930f462e6b8632212.blabla Troj.Win32.TRX.XXPE50FFF026 c:\users\administrator\desktop\downloader\download\     TRENDX  172.20.4.179    Administrator   QllZad  2.0.0.0 5bfa1989e2f6e4a6af9ff62930f462e6b8632212        AAAAAAAAAAQAIQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=    
5
  • It is possible - your log file is just a white space separated file and you could turn that into a csv file easily enough if that helps (or not). What code have you tried already? What makes you think it's not possible, or what problems have you already encountered? Commented Oct 2, 2018 at 10:37
  • because I'm given a task to match my csv file to lof file, that's why Commented Oct 2, 2018 at 10:40
  • what you're saying is that I should convert the log file into a csv? Commented Oct 2, 2018 at 10:41
  • tried it just now, and didn't worked Commented Oct 2, 2018 at 10:42
  • 1
    You need one of these. Either you'll solve your own problem in creating it, or people will be better able to help you. If you have code that you've tried, tidy it up (if it needs it) and post it. Commented Oct 2, 2018 at 13:20

1 Answer 1

1

Yes, it is possible. Something like this worked for me:

import csv

logfile = open('test_log.log', 'r') 
log = logfile.read().split(" ") #Read the log file and split on space

with open('data.csv','r') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        if line[0] in log:
            print(line[0])

Note that if you want to write back to the csv, you need to read the whole csv, make changes, then write to file.

Sign up to request clarification or add additional context in comments.

2 Comments

tried this, but didn't worked pastebin.com/0Yc6JVs5 take a look at this, my log file
What did you try and did you get out? This code is just an example to show how you can match a field in the csv file to a string in the log file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.