Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89992
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Writing list of dictionaries to CSV |
| Organization | Decebal Computing |
| References | <5bd0afb3-d2d4-469f-be39-5869f01adc92@googlegroups.com> |
| Date | 2015-05-05 20:44 +0200 |
| Message-ID | <87vbg6g8zj.fsf@Equus.decebal.nl> (permalink) |
Op Tuesday 5 May 2015 19:09 CEST schreef Kashif Rana:
> When I am writing list of dictionaries to CSV file, the key
> 'schedule' has value 'Mar 2012' becomes Mar-12. I really do not have
> clue why thats happening. Below is the code.
>
> dic_1 = {'action': 'permit', 'dst-address': 'maxprddb-scan-167,
> maxprddb-scan-168, maxprddb-scan-169', 'from': 'DMZ Web', 'id':
> '1000', 'log': 'Enable, session-init', 'name': 'Test Rule Temporary
> ', 'service': '1521, Oraccle-Maximo-Scan-1550, PING-ALL',
> 'src-address': 'sparkeregap1, sparkeregap2', 'to': 'Trust'}
>
> dic_2 {'action': 'permit',
> 'dst-address': 'sparkcas01, sparkcas02, email.ab.spark.net',
> 'from': 'DMZ Web',
> 'id': '4000',
> 'log': 'Enable, session-init',
> 'schedule': 'Mar 2012',
> 'service': 'SMTP',
> 'src-address': 'sparkeregap1, sparkeregap2',
> 'to': 'Trust'}
>
> my_list = [{'to': 'Trust', 'service': '1521,
> Oraccle-Maximo-Scan-1550, PING-ALL', 'from': 'DMZ Web',
> 'dst-address': 'maxprddb-scan-167, maxprddb-scan-168,
> maxprddb-scan-169', 'name': 'Test Rule Temporary ', 'action':
> 'permit', 'id': '1000', 'src-address': 'sparkeregap1, sparkeregap2',
> 'log': 'Enable, session-init'}, {'to': 'Trust', 'from': 'DMZ Web',
> 'dst-address': 'sparkcas01, sparkcas02, email.ab.spark.net',
> 'service': 'SMTP', 'schedule': 'Mar 2012', 'action': 'permit', 'id':
> '4000', 'src-address': 'sparkeregap1, sparkeregap2', 'log': 'Enable,
> session-init'}]
>
> pol_keys = ['id', 'name', 'from', 'to', 'src-address',
> 'dst-address', 'service', 'action', 'nat_status', 'nat_type',
> 'nat_src_ip', 'nat_dst_ip', 'nat_dst_port', 'log', 'schedule']
>
> with open('test.csv', 'wb') as f:
> w = csv.DictWriter(f, pol_keys)
> w.writeheader()
> w.writerows(my_list)
Well, I get:
id,name,from,to,src-address,dst-address,service,action,nat_status,nat_type,nat_src_ip,nat_dst_ip,nat_dst_port,log,schedule
1000,Test Rule Temporary ,DMZ Web,Trust,"sparkeregap1, sparkeregap2","maxprddb-scan-167, maxprddb-scan-168, maxprddb-scan-169","1521, Oraccle-Maximo-Scan-1550, PING-ALL",permit,,,,,,"Enable, session-init",
4000,,DMZ Web,Trust,"sparkeregap1, sparkeregap2","sparkcas01, sparkcas02, email.ab.spark.net",SMTP,permit,,,,,,"Enable, session-init",Mar 2012
So there is something different on your system.
By the way: next time make sure that we can copy paste your code:
- dic_2 does not have a '='
- get rid of the space before my_list
- make sure the '[' is at the same line as my_list (or use a \)
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Writing list of dictionaries to CSV Kashif Rana <kashifrana84@gmail.com> - 2015-05-05 10:09 -0700
Re: Writing list of dictionaries to CSV MRAB <python@mrabarnett.plus.com> - 2015-05-05 19:11 +0100
Re: Writing list of dictionaries to CSV Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-05 13:25 -0500
Re: Writing list of dictionaries to CSV Cecil Westerhof <Cecil@decebal.nl> - 2015-05-05 20:44 +0200
Re: Writing list of dictionaries to CSV Matthew Ruffalo <mmr15@case.edu> - 2015-05-05 15:43 -0400
Re: Writing list of dictionaries to CSV Tim Chase <python.list@tim.thechases.com> - 2015-05-05 12:34 -0500
Re: Writing list of dictionaries to CSV Kashif Rana <kashifrana84@gmail.com> - 2015-05-05 22:32 -0700
Re: Writing list of dictionaries to CSV Chris Angelico <rosuav@gmail.com> - 2015-05-06 16:34 +1000
Re: Writing list of dictionaries to CSV Cecil Westerhof <Cecil@decebal.nl> - 2015-05-06 08:50 +0200
Re: Writing list of dictionaries to CSV Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-05-06 09:14 -0400
Re: Writing list of dictionaries to CSV MRAB <python@mrabarnett.plus.com> - 2015-05-06 19:08 +0100
Re: Writing list of dictionaries to CSV Tim Chase <python.list@tim.thechases.com> - 2015-05-06 13:22 -0500
Re: Writing list of dictionaries to CSV Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-06 12:27 -0600
Re: Writing list of dictionaries to CSV Tim Chase <python.list@tim.thechases.com> - 2015-05-06 13:37 -0500
Re: Writing list of dictionaries to CSV Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-06 23:31 +0000
Re: Writing list of dictionaries to CSV Tim Chase <python.list@tim.thechases.com> - 2015-05-06 20:22 -0500
Re: Writing list of dictionaries to CSV Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-07 01:55 +0000
Re: Writing list of dictionaries to CSV [correction] Tim Chase <python.list@tim.thechases.com> - 2015-05-06 20:52 -0500
csiph-web