0

I am a beginner at Python and i am trying to write a script convert a text file to csv. The format of the txt logs is like below:

"Number" "Date" "Time" "Interface" "Origin" "Type" "Action" "Service" "Source Port" "Source" "Destination" "Protocol" "Rule" "Rule Name" "Current Rule Number" "User" "Information" "Product" "Source Machine Name" "Source User Name"

"176" "16Oct2017" "23:59:00" "eth1" "FWSIN2" "Log" "Accept" "TCP_135" "62005" "Host_10.2.2.68" "10.168.150.135" "tcp" "271" "" "271-SINFW" "" "inzone: Internal; outzone: External; service_id: TCP_135" "Security Gateway/Management" "" ""

I have written the below script (in python3) to do this, but it does not seem to work; it prints well on screen, but prints None in file. How do i change this code to fix this?

import shlex

socfile=open('samplelogs.txt',encoding='utf-8')
csvfile=open('csvfile.csv',mode='w',encoding='utf-8')
for strlist in socfile:
    str=shlex.split(strlist)
    for i in str:
        myline=print(i,',',end='')
    csvfile.write("%s" % myline)
    #print(myline)
    
socfile.close()
csvfile.close()
4
  • 1
    Check your indentation. As posted, the line beginning csvfile.write is outside the loop, i.e. it will only execute once with the last value of myline. Commented Nov 2, 2017 at 13:13
  • Try using with statement when working woth unmanaged resources. Commented Nov 2, 2017 at 13:17
  • For converting this file to csv, you might be better off looking up how to convert tsv to csv with bash. It escapes me at the moment. Commented Nov 2, 2017 at 13:22
  • print(...) returns None. Commented Nov 2, 2017 at 14:22

3 Answers 3

2

"print" function does not return string, it prints a string to a file. Here is it's signature:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

'myline' is always a 'None' value. Try this instead:

import shlex

socfile=open('test.txt',encoding='utf-8')
csvfile=open('csvfile.csv',mode='w',encoding='utf-8')
for strlist in socfile:
    str=shlex.split(strlist)
    for i in str:
        print(i,',',end='', file=csvfile)
    #csvfile.write("%s" % myline)
    #print(myline)

socfile.close()
csvfile.close()
Sign up to request clarification or add additional context in comments.

Comments

1

The input file seems to be a blank separated file with fields optionaly enclosed in double quotes. This is easy to parse with the csv module itself:

with open('samplelogs.txt',encoding='utf-8', newline='') as socfile, \
        open('csvfile.csv',mode='w',encoding='utf-8', newline='') as csvfile:
    rd = csv.reader(socfile, delimiter = ' ', quoting=csv.QUOTE_ALL)   # or "\t" if the delimiter is a tab
    wr = csv.writer(csvfile)
    for row in rd:
        wr.writerow(row)

Comments

1

You can use the csv module with dialects to read and write your files. It'll be less prone to error not rewriting csv-handling code yourself.

Addressing your bug, do this instead:

csvfile.write(','.join(str) + '\n')

Here's your entire program rewritten to be more pythonic. It doesn't include quotes around the fields, but you could add them yourself. But then, just use the csv module and let it do everything for you.

import shlex

with open('test.txt', encoding='utf-8') as socfile:
    with open('csvfile.csv', mode='w', encoding='utf-8') as csvfile:
        csvfile.writelines(','.join(shlex.split(line)) + '\n' for line in socfile)

Here's a complete example using the csv module:

#!/usr/bin/env python3

import csv


def convert(space_separated_file, csv_file):
    class unix_space(csv.unix_dialect):
        def __init__(self):
            self.delimiter = ' '

    input_rows = csv.reader(space_separated_file, dialect=unix_space())
    output = csv.writer(csv_file, dialect='unix')
    output.writerows(input_rows)


def example(in_filename, out_filename):
    with open(in_filename) as f_in:
        with open(out_filename, "w") as f_out:
            convert(f_in, f_out)


def test():
    with open('test.txt', 'w') as f:
        f.write('''"Number" "Date" "Time" "Interface" "Origin" "Type" "Action" "Service" "Source Port" "Source" "Destination" "Protocol" "Rule" "Rule Name" "Current Rule Number" "User" "Information" "Product" "Source Machine Name" "Source User Name"
"176" "16Oct2017" "23:59:00" "eth1" "FWSIN2" "Log" "Accept" "TCP_135" "62005" "Host_10.2.2.68" "10.168.150.135" "tcp" "271" "" "271-SINFW" "" "inzone: Internal; outzone: External; service_id: TCP_135" "Security Gateway/Management" "" ""
''')

    example('test.txt', 'test.csv')

    with open('test.csv') as f:
        print(f.read())


test()

Output:

"Number","Date","Time","Interface","Origin","Type","Action","Service","Source Port","Source","Destination","Protocol","Rule","Rule Name","Current Rule Number","User","Information","Product","Source Machine Name","Source User Name"
"176","16Oct2017","23:59:00","eth1","FWSIN2","Log","Accept","TCP_135","62005","Host_10.2.2.68","10.168.150.135","tcp","271","","271-SINFW","","inzone: Internal; outzone: External; service_id: TCP_135","Security Gateway/Management","",""

Your output:

Number,Date,Time,Interface,Origin,Type,Action,Service,Source Port,Source,Destination,Protocol,Rule,Rule Name,Current Rule Number,User,Information,Product,Source Machine Name,Source User Name
176,16Oct2017,23:59:00,eth1,FWSIN2,Log,Accept,TCP_135,62005,Host_10.2.2.68,10.168.150.135,tcp,271,,271-SINFW,,inzone: Internal; outzone: External; service_id: TCP_135,Security Gateway/Management,,

Comments

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.