Xlsxwriter中是否有csv.DictWriter?

时间:2015-05-11 00:51:44

标签: python excel csv dictionary xlsxwriter

我有一个程序可以做到这一点:

我读了一些信息,而不是把它放到字典中,然后我将这些字典附加到列表中,所以我有一个字典列表......

我在做的是:使用csv函数,csv.DictWriter并创建三个不同的文件。

我正在搜索,我发现了一个名为xlsxwriter的模块,可以用来创建一个EXCEL WORKBOOK。

我搜索了但是我在Xlsxwriter中找不到类似于DictWrite的任何解决方案,有没有人知道是否有?

我做的是一个不太好的解决方法,我并不满意。

如果有人有任何建议,我会很高兴。

这是我的代码:

#!/usr/bin/env python
import csv, collections, xlsxwriter

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return "ERROR"

def process_tac_dump():
    f = open(files)
    for lines in f:
        if 'ERS.VA' == lines[39:45]:
            d = (   ('mnc', lines[12:15]),
                    ('tac-lb', lines[52:54]),
                    ('tac-hb', lines[61:63]),
                    ('TAC', lines[61:63]+lines[52:54]),
                    ('TAC_DEC', str(int(lines[61:63]+lines[52:54],16))),
                    ('GGSN', find_between(lines,'vip-s11.', '.node.epc' )),
                    ('ORDER', find_between(lines, 'IN NAPTR ', ' 65525')),
                    ('Servicos', find_between(lines, 'x-3gpp-sgw:', '" "" topoff'))
                )

            d = collections.OrderedDict(d)

            if '002' in d['mnc']:
                if "ERROR" in d['GGSN']:
                    pass
                elif "x-s11:x-s8-gtp" not in d['Servicos']:
                    mnc02.append(d)
                    #Procedimento de configuração novo
                    delete_tac_naptrrecord_mnc02.append('delete naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -where container=GPRS')
                    create_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s11.GPRJO2.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    create_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s11.'+d['GGSN']+'.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    #Procedimento de FallBack
                    fallback_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s5s8.GPRJO2.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    fallback_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc02.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')

            elif '003' in d['mnc']:
                if "ERROR" in d['GGSN']:
                    pass
                elif "x-s11:x-s8-gtp" not in d['Servicos']:
                    mnc03.append(d)
                    #Procedimento de configuração novo
                    delete_tac_naptrrecord_mnc03.append('delete naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -where container=GPRS')
                    create_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s11.GPSPO2.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    create_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s11.'+d['GGSN']+'.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    #Procedimento de FallBack
                    fallback_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s5s8.GPSPO2.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    fallback_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc03.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')

            elif '004' in d['mnc']:
                if "ERROR" in d['GGSN']:
                    pass
                elif "x-s11:x-s8-gtp" not in d['Servicos']:
                    mnc04.append(d)
                    #Procedimento de configuração novo
                    delete_tac_naptrrecord_mnc04.append('delete naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -where container=GPRS')
                    create_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s11.GPRJO2.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')
                    create_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s11.'+d['GGSN']+'.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')
                    #Procedimento de FallBack
                    fallback_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order=10;preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s8-gtp";regexp="";replacement=topoff.vip-s5s8.GPRJO2.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc002.mcc724.3gppnetwork.org.;container=GPRS')
                    #fallback_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc003.mcc724.3gppnetwork.org.;container=GPRS')
                    fallback_naptrrecord_mnc04.append('create naptrrecord tac-lb'+d['tac-lb']+'.tac-hb'+d['tac-hb']+'.tac.epc.mnc'+d['mnc']+'.mcc724.3gppnetwork.org. -set order='+d['ORDER']+';preference=65525;flags="a";service="x-3gpp-sgw:x-s11:x-s5-gtp";regexp="";replacement=topoff.vip-s5s8.'+d['GGSN']+'.node.epc.mnc004.mcc724.3gppnetwork.org.;container=GPRS')




#Listas para configuração S11
delete_tac_naptrrecord_mnc02 = []
delete_tac_naptrrecord_mnc03 = []
delete_tac_naptrrecord_mnc04 = []
create_naptrrecord_mnc02 = []
create_naptrrecord_mnc03 = []
create_naptrrecord_mnc04 = []
create_node_arecord_vip_s11 = []

#Listas para Fallback
fallback_naptrrecord_mnc02 = []
fallback_naptrrecord_mnc03 = []
fallback_naptrrecord_mnc04 = []
fallback_delete_tac_naptrrecord_mnc02 = []
fallback_delete_tac_naptrrecord_mnc03 = []
fallback_delete_tac_naptrrecord_mnc04 = []

mnc02 =[]
mnc03 =[]
mnc04 =[]

files = 'teste.txt'
workbook = xlsxwriter.Workbook('TAC_DUMP.xlsx')
worksheet_script_mnc02 = workbook.add_worksheet('MNC02')
worksheet_script_mnc03 = workbook.add_worksheet('MNC03')
worksheet_script_mnc04 = workbook.add_worksheet('MNC04')
worksheet_mnc02 = workbook.add_worksheet('MNC02_TAC')
worksheet_mnc03 = workbook.add_worksheet('MNC03_TAC')
worksheet_mnc04 = workbook.add_worksheet('MNC04_TAC')

#Processa a função principal
process_tac_dump()

mnc_02_path = 'tac_mnc02.csv'
keys_mnc02 = mnc02[0].keys()
with open(mnc_02_path,'wb') as resultado: 
    dict_writer = csv.DictWriter(resultado, keys_mnc02)
    dict_writer.writeheader()
    dict_writer.writerows(mnc02)

mnc_03_path = 'tac_mnc03.csv'
keys_mnc03 = mnc03[0].keys()
with open(mnc_03_path,'wb') as resultado: 
    dict_writer = csv.DictWriter(resultado, keys_mnc03)
    dict_writer.writeheader()
    dict_writer.writerows(mnc03)

mnc_04_path = 'tac_mnc04.csv'
keys_mnc04 = mnc04[0].keys()
with open(mnc_04_path,'wb') as resultado: 
    dict_writer = csv.DictWriter(resultado, keys_mnc04)
    dict_writer.writeheader()
    dict_writer.writerows(mnc04)

mnc_02_gambiarra = [] 
with open (mnc_02_path) as f:
    for lines in f:
        mnc_02_gambiarra.append(lines)

mnc_03_gambiarra = [] 
with open (mnc_03_path) as f:
    for lines in f:
        mnc_03_gambiarra.append(lines)

mnc_04_gambiarra = [] 
with open (mnc_04_path) as f:
    for lines in f:
        mnc_04_gambiarra.append(lines)



worksheet_script_mnc02.write_column('A1', create_naptrrecord_mnc02)
worksheet_script_mnc03.write_column('A1', create_naptrrecord_mnc03)
worksheet_script_mnc04.write_column('A1', create_naptrrecord_mnc04)
worksheet_mnc02.write_column('A1', mnc_02_gambiarra)
worksheet_mnc03.write_column('A1', mnc_03_gambiarra)
worksheet_mnc04.write_column('A1', mnc_04_gambiarra)
workbook.close()

1 个答案:

答案 0 :(得分:2)

请试用我的包装库pyexcel,它不能直接帮助你,但是库提供了一个读取,编写csv和xlsx文件的接口。

以下是示例代码:

>>> from collections import OrderedDict
>>> import pyexcel as pe # csv support by default
>>> import pyexcel.ext.xlsx # for xlsx format support, pip install pyexcel-xlsx
>>> # import pyexcel.ext.xls # for xls format support, pip install pyexcel-xls
>>> # import pyexcel.ext.ods # for xls format support, pip install pyexcel-ods
>>> sample_dict = OrderedDict()
>>> sample_dict.update({"Column 1": [1,2,3,4,5]})
>>> sample_dict.update({"Column 2": [6,7,8,9,10]})
>>> pe.save_as(adict=sample_dict, out_file="your_file.xlsx")
>>> pe.save_as(adict=sample_dict, out_file="your_file.csv")
>>> # pe.save_as(adict=sample_dict, out_file="your_file.xls")
>>> # pe.save_as(adict=sample_dict, out_file="your_file.ods")