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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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...)