Writing list of dictionaries to CSV

Kashif Rana kashifrana84 at gmail.com
Wed May 6 01:32:28 EDT 2015


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

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)



More information about the Python-list mailing list