Read CSV to dictionary

This is the place for queries that don't fit in any of the other categories.

Read CSV to dictionary

Postby ms4sman » Sun Feb 17, 2013 8:11 pm

Alright, so I have another question now. For a school-related project, I am making an evaluation program. It has to save results to a CSV file. I have never worked with CSV before, so this is all new to me. I have figured out how to write the data to file however. But I also have to be able to generate a report with the data from the CSV. I am going to use a ListCtrl. Maybe this isnt the most efficient way, but I want to populate the ListCtrl from a dictionary, (one dict per line), and I want to make the dictionaries from the CSV file (also one dict per line). Is this the best way to fill a listctrl from a CSV? Is there a way to fill dictionaries from CSV? Is there a way to populate a ListCtrl from dictionaries? Thanks!
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby Yoriz » Sun Feb 17, 2013 8:47 pm

There is a CSV module in the python standard library and it has a DictReader & DictWriter methods read up on it here
Check out Objectlistview for showing list data, it makes it much easier to fill out the list from a dictionary.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 723
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Read CSV to dictionary

Postby ms4sman » Sun Feb 17, 2013 8:58 pm

Looks like Ive got some reading to do! Thanks! BTW, I was planning to use ObjectListView already for some other parts, but didn't realize it could fill from a dict as well. That is even better! Thanks!
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby Yoriz » Sun Feb 17, 2013 9:15 pm

ms4sman wrote:Looks like Ive got some reading to do! Thanks! BTW, I was planning to use ObjectListView already for some other parts, but didn't realize it could fill from a dict as well. That is even better! Thanks!


Take a look in the examples folder that comes with objectlistview, there is a python file UsingDictionaryExample.py and there is various others in there too if you didn't know about them.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 723
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 12:30 am

I don't think this really warrants a new thread, so I will just put it here since it is still a branch off the original question.

IOError : [Errno 22] invalid mode ('rb') or filename : 'data\x0cield_placements.txt'
1118: main_frame.py: ee_report = reports.report(self)
246: reports.py: self.load_placements(parent)
215: reports.py: csv_file = csv.DictReader(open(eval_file, 'rb'), delimiter=',', Quotechar="""

I think all pertinent lines are included in the traceback except these:
Code: Select all
    def load_placements(self, prnt):
        eval_file = 'data\field_placements.txt'
        csv_file = csv.DictReader(open(eval_file, 'rb'), delimiter=',', quotechar='"')
        for line in csv_file:
            empe_num = line['empe_num']
            empr_num = line['empr_num']


The odd thing here is that the file I told it to load is not calld x0cield_placements.txt, its called field_placements.txt. I don't understand why it is reading it that way.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby stranac » Mon Feb 18, 2013 12:36 am

Python is interpreting '\f' as an escape sequence.
To get a backslash followed by an 'f', use either '\\f' or r'\f'.

But here you don't need to.
You should always use forward slashes in paths.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 1:27 am

Thank you that makes sense.
Back on the topic of ObjectListView, the way I decided to code it, I ended up loading the values into a list instead of a dictionary. I don't see a way to do it, although I am sure there is. How would I populate the listctrl from a list instead of a dictionary?

EDIT: I thought maybe I should clarify what I am trying to do. I have one list for each column in my CSV file. I want to take those list/columns and use them to populate a ListCtrl.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby mouseroot » Mon Feb 18, 2013 2:25 am

try using list(dict.items()) or list(dict.keys())
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 2:42 am

I'm sorry, but I'm not sure what you mean. Here is an example of loading the values from the CSV file.
Code: Select all
        eval_file = 'data\evaluation_results.txt'
        csv_file = csv.DictReader(open(eval_file, 'rb'), delimiter=',', quotechar='"')
        for line in csv_file:
            eval_num = line['eval_num']
            empe_num = line['empe_num']
            empr_num = line['empr_num']
            eval_date = line['eval_date']
            next_eval = line['next_eval']
            qual_score = line['qual_score']
            qual_comm = line['qual_comm']
            habits_score = line['habits_score']
            habits_comm = line['habits_comm']
            knowledge_score = line['knowledge_score']
            knowledge_comm = line['knowledge_comm']
            behavior_score = line['behavior_score']
            behavior_comm = line['behavior_comm']
            ave_score = line['ave_score']
            overall_score = line['overall_score']
            overall_comm = line['overall_comm']
            emp_rec = line['emp_rec']


And here is an example CSV file with one record in it:
"eval_num","empe_num","empr_num","eval_date","next_eval","qual_score","qual_comm","habits_score","habits_comm","knowledge_score","knowledge_comm","behavior_score","behavior_comm","ave_score","overall_score","overall_comm","emp_rec"
"5998","0005","0027","02-17-13","02/17/13 13:09:49","4","Generally no mistakes","4","Occasionally gets distracted","5","Very intelligent about job","3","Often gets in arguments","4.0","4","Overall, a good worker with a few quirks","False"
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby mouseroot » Mon Feb 18, 2013 3:00 am

if all your trying todo is populate a list control with a list
you can do something like this
Code: Select all
list_items = []
for line in csv_file:
    for key,val in line:
        list_items.append(val)


maybe im not understanding what your trying to accomplish
but I feel like this may help.
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 3:04 am

Maybe I'm misunderstanding you. I already have the CSV stuff loaded into the lists. one list for every column. Now i am trying to use objectlistview to populate a list control with those lists, one list for each column. Maybe that is what you said, but I guess I don't understand.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby mouseroot » Mon Feb 18, 2013 3:18 am

I misunderstood you are using wxPython and a wrapper around wx.ListCtrl
I haven't actually done any wxPython in a while but im reading the docs

according to http://wxpython.org/docs/api/wx.ListCtrl-class.html
you can use
Code: Select all
listControl.append(value)

in your loop
also you can create a dict from two lists (one for keys, and one for values)
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 3:38 am

ok, so say I am not going to use dictionaries. I would just do this?
Code: Select all
listControl.append(list_name)

because unless I am mistaken, that will create a row, with all of the values that should be in a column?
maybe I'm just being dense...
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby mouseroot » Mon Feb 18, 2013 3:46 am

well I was just trying to help I dont have wxPython installed atm
but I may play around with it later
mouseroot
 
Posts: 17
Joined: Sat Feb 16, 2013 1:14 am

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 3:48 am

Well, thank you for trying. I'll keep at it.
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby ms4sman » Mon Feb 18, 2013 5:05 am

OK, if anyone knows how to do this please help:

I am loading values from a CSV file. I want to load them into a dictionary, instead I ended up with this:
Code: Select all
        eval_file = 'data\evaluation_results.txt'
        csv_file = csv.DictReader(open(eval_file, 'rb'), delimiter=',', quotechar='"')
        for line in csv_file:
            eval_num = line['eval_num']
            empe_num = line['empe_num']
            empr_num = line['empr_num']
            eval_date = line['eval_date']
            next_eval = line['next_eval']
            qual_score = line['qual_score']
            qual_comm = line['qual_comm']
            habits_score = line['habits_score']
            habits_comm = line['habits_comm']
            knowledge_score = line['knowledge_score']
            knowledge_comm = line['knowledge_comm']
            behavior_score = line['behavior_score']
            behavior_comm = line['behavior_comm']
            ave_score = line['ave_score']
            overall_score = line['overall_score']
            overall_comm = line['overall_comm']
            emp_rec = line['emp_rec']
           


I know that is not even close to what I want, but I don't know how to load it right. Here is the CSV:

"eval_num","empe_num","empr_num","eval_date","next_eval","qual_score","qual_comm","habits_score","habits_comm","knowledge_score","knowledge_comm","behavior_score","behavior_comm","ave_score","overall_score","overall_comm","emp_rec"
"5998","0005","0027","02-17-13","02/17/13 13:09:49","4","Generally no mistakes","4","Occasionally gets distracted","5","Very intelligent about job","3","Often gets in arguments","4.0","4","Overall, a good worker with a few quirks","False"

Once in a dictionary, I want to use the dictionaries to populate a ListCtrl somehow. It could be with objectlistview, or not. I don't really care.

If you know how to do this, please help!
ms4sman
 
Posts: 24
Joined: Sat Feb 16, 2013 5:55 pm

Re: Read CSV to dictionary

Postby Yoriz » Mon Feb 18, 2013 6:49 am

New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 723
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests