Python Forum
code help to convert log file to csv format - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: code help to convert log file to csv format (/thread-15500.html)



code help to convert log file to csv format - bmunagala - Jan-19-2019

Need to convert a logfile that will have some information into a csv format.

Sample Data:
Output:
Info type: PERSON_NAME; Count: 25 Info type: LOCATION; Count: 25 Info type: CREDIT_CARD_NUMBER; Count: 2 Info type: EMAIL_ADDRESS; Count: 150 Info type: US_VEHICLE_IDENTIFICATION_NUMBER; Count: 20 Info type: MALE_NAME; Count: 13 Info type: LAST_NAME; Count: 85 Info type: DOMAIN_NAME; Count: 150 Info type: SSN; Count: 1000
I need this to be converted in to csv format. I am trying with the below logic but
import os
import sys
import csv


ip_file=r"C:\Users\bmunagala\Desktop\Output.log"
op_file=ip_file.split(".")

print("Opening " + ip_file)

f0 = open(ip_file,"r")
lstInput = []
for oLine in f0:
    try:
        lstLine = oLine.replace("\n","").split("Info type:")
    except Exception as e:
        print(e)
        pass
    lstInput.append(lstLine)
    
f0.close()

fw = open(op_file[0] + ".csv", "w")
for oLine in lstInput:
    szWriteLine = ",".join(oLine)
    fw.write(szWriteLine + "\n")
fw.close()
But output is like this
Output:
, PERSON_NAME; Count: 25 , LOCATION; Count: 25 , CREDIT_CARD_NUMBER; Count: 2 , EMAIL_ADDRESS; Count: 150 , US_VEHICLE_IDENTIFICATION_NUMBER; Count: 20 , MALE_NAME; Count: 13 , LAST_NAME; Count: 85 , DOMAIN_NAME; Count: 150 , SSN; Count: 1000



RE: code help to convert log file to csv format - snippsat - Jan-19-2019

Example,and i didn't see the point with the starting ,.
with open('log1.txt') as f,open('out.csv', 'w') as f_out:
    for line in f:
        line = line.strip().split('Info type: ')[1]
        f_out.write(f'{line}\n')
Output:
PERSON_NAME; Count: 25 LOCATION; Count: 25 CREDIT_CARD_NUMBER; Count: 2 EMAIL_ADDRESS; Count: 150 US_VEHICLE_IDENTIFICATION_NUMBER; Count: 20 MALE_NAME; Count: 13 LAST_NAME; Count: 85 DOMAIN_NAME; Count: 150 SSN; Count: 1000



RE: code help to convert log file to csv format - bmunagala - Jan-21-2019

Hi I am trying to run with the above code but getting the below error

TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper


RE: code help to convert log file to csv format - snippsat - Jan-21-2019

What's your input and what version of Python do you use?
In my code i copy this from your first post,and save(utf-8) as log1.txt.
Output:
Info type: PERSON_NAME; Count: 25 Info type: LOCATION; Count: 25 Info type: CREDIT_CARD_NUMBER; Count: 2 Info type: EMAIL_ADDRESS; Count: 150 Info type: US_VEHICLE_IDENTIFICATION_NUMBER; Count: 20 Info type: MALE_NAME; Count: 13 Info type: LAST_NAME; Count: 85 Info type: DOMAIN_NAME; Count: 150 Info type: SSN; Count: 1000
Then running code in my post gives this in out.csv:
Output:
PERSON_NAME; Count: 25 LOCATION; Count: 25 CREDIT_CARD_NUMBER; Count: 2 EMAIL_ADDRESS; Count: 150 US_VEHICLE_IDENTIFICATION_NUMBER; Count: 20 MALE_NAME; Count: 13 LAST_NAME; Count: 85 DOMAIN_NAME; Count: 150 SSN; Count: 1000