Python Forum
code help to convert log file to csv format
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code help to convert log file to csv format
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 788 Feb-29-2024, 12:30 AM
Last Post: Winfried
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 742 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 696 Sep-05-2023, 12:50 PM
Last Post: ToniE
  Convert File to Data URL michaelnicol 3 1,146 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 2,494 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 1,981 Dec-17-2022, 12:24 AM
Last Post: snippsat
  Convert Excel file into csv with Pipe symbol.. mg24 4 1,323 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Convert Json to table format python_student 2 5,442 Sep-28-2022, 12:48 PM
Last Post: python_student
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 2,539 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  Convert .xlsx to Format as Table bnadir55 0 888 Aug-11-2022, 06:39 AM
Last Post: bnadir55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020