473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to create csv file format using the list of dictionaries

Hi all,

Further to my 'list of dictionaries' question yesterday, I would to ask how to write data to csv file format using 'list of dictionaries' as a source:

data to be written to .csv file:
dictList = [ {'FirstName': 'Michael', 'LastName': 'Kirk', 'SSID': '224567'},
{'FirstName': 'Linda', 'LastName': 'Matthew', 'SSID': '123456'},
{'FirstName': 'Sandra', 'LastName': 'Parkin', 'SSID': '123456'},
{'FirstName': 'Bob', 'LastName': 'Henry', 'SSID': '666666'},
{'FirstName': 'Silvia', 'LastName': 'Perkin', 'SSID': '676767'}]

so the csv file would look something like:
Michael,Kirk,224567
Linda,Matthew,123456
Sandra,Parkin,123456
Bob,Henry,666666
Silvia,Perkin,676767

What I have in mind for the code would be something like this:

import csv

dictList = [ {'FirstName': 'Michael', 'LastName': 'Kirk', 'SSID': '224567'},
{'FirstName': 'Linda', 'LastName': 'Matthew', 'SSID': '123456'},
{'FirstName': 'Sandra', 'LastName': 'Parkin', 'SSID': '123456'},
{'FirstName': 'Bob', 'LastName': 'Henry', 'SSID': '666666'},
{'FirstName': 'Silvia', 'LastName': 'Perkin', 'SSID': '676767'}]

writer = csv.writer(file('csv_test.CSV','wb'))

for item in dictList:
writer.writerow(item['FirstName'],item['LastName'],item['SSID'])


I am not sure whether it will work out well, but any suggestion(s)/comment(s) are welcomed.

Thanks!
Mar 12 '08 #1
3 3533
jlm699
314 100+
You're on the right track.. I've never used the csv module however, as I find it just as easy to do the following:
Expand|Select|Wrap|Line Numbers
  1. fh = open('csvTest.csv', 'wb')
  2.  
  3. for elem in dictList:
  4.     fh.write('%s,%s,%s\n' % (elem['FirstName'], elem['LastName'], elem['SSID']
  5.  
  6. fh.close()
But there is nothing wrong with using the CSV module, I've just never used it myself. You are on the right track though.
Mar 12 '08 #2
bvdet
2,851 Expert Mod 2GB
I agree with jlm699 (he's pretty sharp). I have never used the csv module either since most of my applications are simple. Here's some more sample code:
Expand|Select|Wrap|Line Numbers
  1. output = [','.join((item['FirstName'], item['LastName'], item['SSID'])) for item in dictList]
  2. f = open('output.csv', 'w')
  3. f.write('\n'.join(output))
  4. f.close()
Mar 12 '08 #3
Ah Ok,

I agree with you guys, will prefer simpler method, of course.

Thanks!
Mar 12 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, my program builds a dictionary that I would like to save in a file. My question is what are the simple ways to do it? The first solution...
7
by: RFQ | last post by:
Hi, I'm struggling here to do the following with any success: I have a comma delimited file where each line in the file is something like: PNumber,3056,Contractor,XYZ Contracting,Architect,ABC...
7
by: Philipp H. Mohr | last post by:
Hello, I would like to store multiple dictionaries in a file, if possible one per line. My code currently produces a new dictionary every iteration and passes it on to another peace of code. In...
6
by: Angelic Devil | last post by:
I'm building a file parser but I have a problem I'm not sure how to solve. The files this will parse have the potential to be huge (multiple GBs). There are distinct sections of the file that I...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: sri2097 | last post by:
Hi all, I'm storing number of dictionary values into a file using the 'cPickle' module and then am retrieving it. The following is the code for it - # Code for storing the values in the file...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
4
by: shigehiro | last post by:
Hi, I am quite new to Python & recently I have been given an assignment to create a list of dictionaries. Can anyone give me an idea of how to do so? Let say, my dictionary will have 3 keys:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.