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: 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
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')
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 . Then running code in my post gives this in out.csv :
|