Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89985

Re: Writing list of dictionaries to CSV

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; "'name':": 0.09; 'assuming': 0.09; 'thats': 0.09; "'test": 0.16; "'to',": 0.16; "(i'm": 0.16; 'csv': 0.16; 'dictionaries': 0.16; 'displaying': 0.16; 'editor,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:CSV': 0.16; 'subject:Writing': 0.16; 'subject:dictionaries': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'file,': 0.19; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'header:In-Reply- To:1': 0.27; 'subject:list': 0.30; "skip:' 10": 0.31; 'file': 0.32; 'text': 0.33; 'becomes': 0.33; 'received:84': 0.35; 'really': 0.36; 'next': 0.36; 'application': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'experts': 0.60; "you're": 0.61; 'temporary': 0.65
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=dZcO3Bne c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=SY9aFQKHIdAA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=dWrDcAwzAAAA:8 a=JKJe4qOOCkDwjfgvK9YA:9 a=HqQfqvNBcK9THNT1:21 a=1QEYj8pkPG57gMt6:21 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Tue, 05 May 2015 19:11:09 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Writing list of dictionaries to CSV
References <5bd0afb3-d2d4-469f-be39-5869f01adc92@googlegroups.com>
In-Reply-To <5bd0afb3-d2d4-469f-be39-5869f01adc92@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.132.1430849471.12865.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430849471 news.xs4all.nl 2941 [2001:888:2000:d::a6]:48635
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89985

Show key headers only | View raw


On 2015-05-05 18:09, Kashif Rana wrote:
> Hello Experts
>
> 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'}
>
Missing '=' in the next line.

> 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)
>
It works OK for me.

(I'm assuming that you're reading the CSV file in a text editor, not
some other application that might be trying to be "clever" by
"interpreting" what it thinks looks a date as a date and then
displaying it differently...)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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