2017-11-02 6 views
0

私はPythonの初心者です。テキストファイルをcsvに変換するスクリプトを作成しようとしています。 txtログのフォーマットは以下のようなものです:変数をPythonでファイルに書き込む

「数」「日付」「時刻」「インタフェース」「起源」「タイプ」「アクション」「サービス」「送信元ポート」「ソース」「宛先」 「プロトコル」「ルール」「ルール名」「現在のルール番号」「ユーザー」「情報」「製品」「ソースマシン名」「ソースユーザー名」「ソースユーザー名」

「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:外部; service_id:TCP_135 ""セキュリティゲートウェイ/管理 "" "" "

これを行うには、以下のスクリプト(in python3)を書いていますが、動作していないようです。それは画面上でうまく印刷されますが、Noneをファイルに印刷します。これを修正するにはどうすればこのコードを変更できますか?

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() 
+1

インデントを確認します。投稿されると、 'csvfile.write'で始まる行はループの外側にあります。つまり、' myline'の最後の値で1回だけ実行されます。 – nekomatic

+0

アンマネージドリソースを扱うときに 'with'ステートメントを使ってみてください。 –

+0

このファイルをcsvに変換するには、tsvをbashでcsvに変換する方法を調べる方がよい場合があります。それは私の瞬間を逃れます。 – HSchmale

答えて

2

"print"関数は文字列を返しません。文字列をファイルに出力します。

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

「マイライン」は常に「なし」の値である: は、ここでの署名です。 代わりにこれを試してみてください:

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() 
1

あなたはあなたのファイルを読み書きする方言でcsvモジュールを使用することができます。 csv-handlingコードを自分で書き直すのではなく、エラーが発生しにくくなります。あなたのバグに対処

、代わりにこれを行う:

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

をここでより多くのニシキヘビに書き換えあなたの全体のプログラムがあります。フィールドの周りに引用符は含まれませんが、あなた自身で追加することができます。しかし、その後、csvモジュールを使用して、すべてのことをあなたに任せましょう。

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) 

ここでcsvモジュールを使用した完全な例です:

#!/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() 

出力:

"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","","" 

あなたの出力:

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,, 
1

入力ファイルは空白のようですフィールドを含むファイルを区切ります。opt二重引用符で囲まれています。これは、csvモジュール自体で解析するのが簡単です。

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) 
関連する問題