Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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; 'feedback.': 0.04; "'name':": 0.09; 'string;': 0.09; 'thats': 0.09; "'test": 0.16; "'to',": 0.16; 'csv': 0.16; 'dictionaries': 0.16; 'editor,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'itself,': 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; ':-)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'file,': 0.19; 'value.': 0.19; 'header:User-Agent:1': 0.23; 'guys': 0.24; 'looks': 0.24; 'excel': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'correct': 0.29; 'subject:list': 0.30; "skip:' 10": 0.31; 'skip:q 20': 0.31; "they'll": 0.31; 'file': 0.32; 'text': 0.33; 'becomes': 0.33; 'could': 0.34; 'problem': 0.35; 'received:84': 0.35; 'but': 0.35; 'really': 0.36; 'thanks': 0.36; 'wrong': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python- list': 0.38; 'quote': 0.39; 'to:addr:python.org': 0.39; 'experts': 0.60; 'tell': 0.60; 'hope': 0.61; 'temporary': 0.65; 'number:': 0.66; '2015': 0.84 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=TPlN-F8qWGGE1QpT1J4A:9 a=m-YrBc9aELs8dH8K:21 a=ALyuZ_bFiRBkAf8T:21 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Wed, 06 May 2015 19:08:52 +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> <072badd6-0542-468a-99bd-cc3a4ffc7156@googlegroups.com> In-Reply-To: <072badd6-0542-468a-99bd-cc3a4ffc7156@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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430935742 news.xs4all.nl 2965 [2001:888:2000:d::a6]:50907 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90066 On 2015-05-06 06:32, Kashif Rana wrote: > Hello guys > > thanks for the feedback. I think its problem with excel itself, showing wrong value. Because when I opened the csv file in text editor, I can see correct value but opening in excel showing wrong value. What I can do to see correct in excel as well. > > Regards > You could tell it to quote any value that's not a number: w = csv.DictWriter(f, pol_keys, quoting=csv.QUOTE_NONNUMERIC) It looks like all of the values you have are strings, so they'll all be quoted. I would hope that Excel will then treat it as a string; it would be stupid if it didn't! :-) > On Tuesday, May 5, 2015 at 9:09:40 PM UTC+4, 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'} >> >> 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) >