hi!

i want to place the contents of a dictionary into a csv file. Can someone help me with this?

i already have the code in reading the csv file

import csv

reader = csv.reader(open("c:\sample.dat"))

for row in reader:
print row

i want the first element of the row be the key for the dictionary so that if i access the dictionary again using the key i'll be able to get the different of the rows of that dictionary. I just don't know how to use loop thru the csv rows and put it in the dictionary and use the first element as a key.

for example: key name is key:
//key as the key

key == first element of the row.

d = {'key': [1,2,3,4,5,6]}

//accessing the dictionary using key and were able to get the other elements of the row
d [1, 2, 3, 4, 5, 6]

can someone help me code this.

Thanks!

Recommended Answers

All 20 Replies

import csv

reader = csv.reader(open("c:\sample.dat"))

d={}
for row in reader:
    d[row[0]]=row[1:]
import csv

reader = csv.reader(open("c:\sample.dat"))

d={}
for row in reader:
    d[row[0]]=row[1:]

hi! your code worked. thanks!
But I have another question:

since the key is the first element of the row, i want to access other elements like this:

d [fieldname]

i can access the elements by doing this d [2]
but i need to know on what position in the array is that element. It would be better to use the fieldname. Can you help with this?


example csv:

123, gishi, gishi@mymail.com, 456 happy st.

fields:
id, name, email, homeaddress

thanks again!

Can't you do like this:

d['key']['fieldname']

Can't you do like this:

d['key']['fieldname']

You can do that if you already have the fieldnames for the csv elements. If you don't have that you can access the elements by using the position of the element in the array.
That is why I was asking for someone to help me write headers for my csv files.

If the fields are in the same position you can tweak a little the read function and add the fields.

Can you post a sample csv?

If the fields are in the same position you can tweak a little the read function and add the fields.

Can you post a sample csv?

example is like this:


123, gishi, gishi@mymail.com, 456 happy st.

fields:
id, name, email, homeaddress

Hope it helps.

import csv

d={}

for row in csv.reader(open('sample.dat')):
    d['ID %s' % row[0]] = {'name': row[1], 'email': row[2], 'homeaddress': row[3]}

print d
print

user_id = 125

print 'ID %s:' % user_id, d['ID %s' % user_id]
print
print 'ID %s' % user_id
print 'Name:', d['ID %s' % user_id]['name'],
print 'Email:', d['ID %s' % user_id]['email'],
print 'Home Address:', d['ID %s' % user_id]['homeaddress']

Output:

>>> 
{'ID 126': {'homeaddress': ' 459 happy st.', 'name': ' gishi4', 'email': ' gishi4@mymail.com'}, 'ID 124': {'homeaddress': ' 457 happy st.', 'name': ' gishi2', 'email': ' gishi2@mymail.com'}, 'ID 125': {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}, 'ID 123': {'homeaddress': ' 456 happy st.', 'name': ' gishi', 'email': ' gishi@mymail.com'}}

ID 125: {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}

ID 125
Name:  gishi3 Email:  gishi3@mymail.com Home Address:  458 happy st.

Also you can use namedtuples, and csv module is not really needed for simple cases.

from collections import namedtuple
filein = open("sample.dat")
datadict={}

headerline = [f.strip() for f in filein.readline().split(',')]
print headerline
Dataline=namedtuple('Dataline',headerline)

for data in filein:
    data=[f.strip() for f in data.split(',')]
    print(data)
    d=Dataline(*data)

    datadict[d.id]=d

print datadict['123'].email
for key,value in  datadict.items():
    print('%s: %s' %(key,value))

input('Ready')

sample.dat

id, name, email, homeaddress
123, gishi, gishi@mymail.com, 456 happy st.
345, tony, tony.veijalainen@somewhere.com, Espoo Finland

Output:

['id', 'name', 'email', 'homeaddress']
['123', 'gishi', 'gishi@mymail.com', '456 happy st.']
['345', 'tony', 'tony.veijalainen@somewhere.com', 'Espoo Finland']
gishi@mymail.com
345: Dataline(id='345', name='tony', email='tony.veijalainen@somewhere.com', homeaddress='Espoo Finland')
123: Dataline(id='123', name='gishi', email='gishi@mymail.com', homeaddress='456 happy st.')
Ready

I thought the same Tony was just starting doing a namedtuple version. Luckly i stoped by to edit my post.

For the csv module I still use because I thought maybe for some csv may have some type of encoding.

I have in the past bitter experience of how compatible output the CSV are and wrote my own CSV module in Delphi, so I do think that for more complicated things CSV module must be great (dialects are not there without reason, I can tell you). I only do not know it so well yet, and I like to hold whole program in my mind. I have not used in my own code namedtuple, so thought it good opportunity to learn how to put it in use and same time maybe help somebody else.

P.S. Have a look at collections source code: the code for namedtuple is fine example how expressive Python is. My namedtuple version looks like candidate for code snippet, for me, what do you think?

My 2 cents
To use csv with other dialect and use dict(zip()) instead of named tuple.

"""
datas.csv :
"123"; "gishi"; "gishi@mymail.com"; "456 happy st."
"345"; "tony"; "tony.veijalainen@somewhere.com"; "Espoo Finland"
"""
import csv
class excel_french(csv.Dialect):
    delimiter=';'
    quotechar='"'
    doublequote=True
    skipinitialspace=False
    lineterminator='\n'
    quoting=csv.QUOTE_MINIMAL

csv.register_dialect('excel_french', excel_french)

header=['id', 'name', 'email', 'homeaddress']
d={}
for row in csv.reader(open('datas.csv'), 'excel_french'):
    drow=dict(zip(header, row))
    d[drow['id']]=drow
print d

>>>
{'123': {'email': ' "gishi@mymail.com"',
         'homeaddress': ' "456 happy st."',
         'id': '123',
         'name': ' "gishi"'},
 '345': {'email': ' "tony.veijalainen@somewhere.com"',
         'homeaddress': ' "Espoo Finland"',
         'id': '345',
         'name': ' "tony"'}}

If the first row of the file is the header line :

"""
datas.csv :
"id"; "name"; "email"; "homeaddress"
"123"; "gishi"; "gishi@mymail.com"; "456 happy st."
"345"; "tony"; "tony.veijalainen@somewhere.com"; "Espoo Finland"
"""
import csv
class excel_french(csv.Dialect):
    delimiter=';'
    quotechar='"'
    doublequote=True
    skipinitialspace=False
    lineterminator='\n'
    quoting=csv.QUOTE_MINIMAL

csv.register_dialect('excel_french', excel_french)

d={}
for i, row in enumerate(csv.reader(open('datas.csv'), 'excel_french')):
    if i==0:
        header=row
    else:
        drow=dict(zip(header, row))
        d[drow['id']]=drow
print d

Hope it helps.

import csv

d={}

for row in csv.reader(open('sample.dat')):
    d['ID %s' % row[0]] = {'name': row[1], 'email': row[2], 'homeaddress': row[3]}

print d
print

user_id = 125

print 'ID %s:' % user_id, d['ID %s' % user_id]
print
print 'ID %s' % user_id
print 'Name:', d['ID %s' % user_id]['name'],
print 'Email:', d['ID %s' % user_id]['email'],
print 'Home Address:', d['ID %s' % user_id]['homeaddress']

Output:

>>> 
{'ID 126': {'homeaddress': ' 459 happy st.', 'name': ' gishi4', 'email': ' gishi4@mymail.com'}, 'ID 124': {'homeaddress': ' 457 happy st.', 'name': ' gishi2', 'email': ' gishi2@mymail.com'}, 'ID 125': {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}, 'ID 123': {'homeaddress': ' 456 happy st.', 'name': ' gishi', 'email': ' gishi@mymail.com'}}

ID 125: {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}

ID 125
Name:  gishi3 Email:  gishi3@mymail.com Home Address:  458 happy st.

Thanks for your answer but the answer i am looking for is like this:

when enter the id: d[address]
it will just give me the output: 456 happy st.

can you help me do this?

Have you tried the examples we gave you ?
Did you simply read them carefully ?
To me, all examples given allow what you've asked for !

I have in the past bitter experience of how compatible output the CSV are and wrote my own CSV module in Delphi, so I do think that for more complicated things CSV module must be great (dialects are not there without reason, I can tell you). I only do not know it so well yet, and I like to hold whole program in my mind. I have not used in my own code namedtuple, so thought it good opportunity to learn how to put it in use and same time maybe help somebody else.

P.S. Have a look at collections source code: the code for namedtuple is fine example how expressive Python is. My namedtuple version looks like candidate for code snippet, for me, what do you think?

I was seeing the collections little time ago, I used the deque on other project.

And yes, just post it, I liked it, pretty good.

Hope it helps.

import csv

d={}

for row in csv.reader(open('sample.dat')):
    d['ID %s' % row[0]] = {'name': row[1], 'email': row[2], 'homeaddress': row[3]}

print d
print

user_id = 125

print 'ID %s:' % user_id, d['ID %s' % user_id]
print
print 'ID %s' % user_id
print 'Name:', d['ID %s' % user_id]['name'],
print 'Email:', d['ID %s' % user_id]['email'],
print 'Home Address:', d['ID %s' % user_id]['homeaddress']

Output:

>>> 
{'ID 126': {'homeaddress': ' 459 happy st.', 'name': ' gishi4', 'email': ' gishi4@mymail.com'}, 'ID 124': {'homeaddress': ' 457 happy st.', 'name': ' gishi2', 'email': ' gishi2@mymail.com'}, 'ID 125': {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}, 'ID 123': {'homeaddress': ' 456 happy st.', 'name': ' gishi', 'email': ' gishi@mymail.com'}}

ID 125: {'homeaddress': ' 458 happy st.', 'name': ' gishi3', 'email': ' gishi3@mymail.com'}

ID 125
Name:  gishi3 Email:  gishi3@mymail.com Home Address:  458 happy st.

thanks for your answer. however what i need is like this:

d[address]

the output would be: 456 happy st.

instead of placing d[2]

can you help me with this again? thanks!

Are you kidding ?
It's just the example I gave 5 post ago !

Don't separate the user id from the entry.

import csv, contextlib

usernfo = {}

with contextlib.closing(open()) as ifile:
    for entry in csv.DictReader(ifile):
        usernfo[entry['id']] = entry

print(usernfo)

Don't separate the user id from the entry.

import csv, contextlib

usernfo = {}

with contextlib.closing(open(data.csv)) as ifile:
    for entry in csv.DictReader(ifile):
        usernfo[entry['id']] = entry

print(usernfo)

Fixed.

Thank you everyone! I'm new to data science using python
Please I have a problem with pandas.DataFrame.read_cvs
"for index, row in iterrow:" doesn't work on Python 2.7 and python 3.4

    However, this is my question:
        I have a cvs file with 5 columns and multiple rows, as shown below:
        `Author_ID      Arrival       Departure         Date                 Time`
          01202             Paris          New York         10/03/2011      10:00
          02122             Beijin         New York         09/03/1999      21:00
          07732             Paris          Kansas              10/03/2011     10:00


          from the table above you can discover that some column values match. I want to extract wherever I find intersection. e.g so as we iterate whenever we encounter a row with the same values in Arrival or Depature or Author_ID and date && Time, we should extract and display 
        Take Arrival column, we have Paris from 2 different Authors, then consider the Date we have 10/03/2011 and "Time" we have 10:00. We want to know if these authors have connections with their depatures, arrivals at particular date and time. 

Please can anyone help?

@Abdulkabir_1 You say that for index, row in iterrow: does'nt work, but we don't know what it means because we don't have your python code. Perhaps you could start a new discussion with your code and the error message sent by python. Also, it would be a good idea to describe more precisely your program's expected output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.