在python中将变量写入文件

时间:2017-11-02 13:11:21

标签: python python-3.x

我是Python的初学者,我正在尝试编写一个脚本,将文本文件转换为csvtxt日志的格式如下:

  

"数" "日期" "时间" "接口" "产地" "类型" "动作" "服务" "源端口" "来源" "目的地" "协议" "规则" "规则名称" "当前规则编号" "用户" "信息" "产品" "源机器名称" "来源用户名"

     

" 176" " 16Oct2017" " 23:59:00" " ETH1" " FWSIN2" "登录" "接受" " TCP_135" " 62005" " Host_10.2.2.68" " 10.168.150.135" " TCP" " 271" "" " 271-SINFW" "" " inzone:内部; 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()

3 个答案:

答案 0 :(得分:2)

“print”函数不返回字符串,它将字符串输出到文件中。 这是它的签名:

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

'myline'始终为'None'值。 试试这个:

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 :(得分:1)

您可以使用带有方言的csv模块来读取和写入文件。它不会出错而不会自己重写csv处理代码。

解决您的错误,请改为:

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

这是你的整个程序被重写为更加pythonic。它不包括字段周围的引号,但您可以自己添加它们。但是,只需使用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,,

答案 2 :(得分:1)

输入文件似乎是一个空白的分隔文件,其中的字段选项用双引号括起来。这很容易用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)