Home > Enterprise >  .log to .csv in python
.log to .csv in python

Time:10-13

I have a .log file in the below format :

A1 B1 C1 D1
A2 B2 C2 D2

I want to make a .csv out of it as :

A  B  C  D #columnname
A1 B1 C1 D1
A2 B2 C2 D2

Note : Without using Pandas & Numpy

CodePudding user response:

Check out the CSV module. Here is the documentation. https://docs.python.org/3/library/csv.html

CodePudding user response:

#!/bin/python
with open('log', 'r') as file:
    data = file.read()
    data = data.split()
columns = ['A', 'B', 'C', 'D']
data = columns   data

toSave = ''
j=1
for i in range(len(data)):
   if (j % 5 == 0):
       toSave = toSave   '\n'   data[i]   ' '
       j=1
   else:
       toSave = toSave   data[i]   ' '
   j =1

f = open("log.csv", "w")
f.write(toSave)
f.close()
  • Related