Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107652 > unrolled thread

how to create a dictionary from csv file?

Started by"+dime+" <donald.ng@gmail.com>
First post2016-04-26 07:18 -0700
Last post2016-04-27 17:37 +0200
Articles 10 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  how to create a dictionary from csv file? "+dime+" <donald.ng@gmail.com> - 2016-04-26 07:18 -0700
    Re: how to create a dictionary from csv file? Peter Otten <__peter__@web.de> - 2016-04-26 16:40 +0200
    Re: how to create a dictionary from csv file? justin walters <walters.justin01@gmail.com> - 2016-04-26 07:51 -0700
    Re: how to create a dictionary from csv file? Tim Chase <python.list@tim.thechases.com> - 2016-04-26 09:36 -0500
      Re: how to create a dictionary from csv file? Hasan Diwan <hasandiwan+usenet@gmail.com> - 2016-04-27 00:36 +0000
    Re: how to create a dictionary from csv file? jfong@ms4.hinet.net - 2016-04-26 19:42 -0700
      Re: how to create a dictionary from csv file? Sibylle Koczian <nulla.epistola@web.de> - 2016-04-27 11:10 +0200
      Re: how to create a dictionary from csv file? Peter Otten <__peter__@web.de> - 2016-04-27 11:31 +0200
      Re: how to create a dictionary from csv file? Sibylle Koczian <nulla.epistola@web.de> - 2016-04-27 17:37 +0200
      Re: how to create a dictionary from csv file? Sibylle Koczian <nulla.epistola@web.de> - 2016-04-27 17:37 +0200

#107652 — how to create a dictionary from csv file?

From"+dime+" <donald.ng@gmail.com>
Date2016-04-26 07:18 -0700
Subjecthow to create a dictionary from csv file?
Message-ID<246c571e-1793-4aa6-9405-19d7a1355598@googlegroups.com>
I am learning python.

if I have a csv file, like this
banana,4.0
apple,3.5
orange,3.0

Can anyone show me how to read the csv file line by line and then create a dictionary to contain these keys and values?


Regards,
+dime+ 

[toc] | [next] | [standalone]


#107654

FromPeter Otten <__peter__@web.de>
Date2016-04-26 16:40 +0200
Message-ID<mailman.109.1461681647.32212.python-list@python.org>
In reply to#107652
+dime+ wrote:

> I am learning python.
> 
> if I have a csv file, like this
> banana,4.0
> apple,3.5
> orange,3.0
> 
> Can anyone show me how to read the csv file line by line and then create a
> dictionary to contain these keys and values?

Below is a spoiler, but learning Python is more fun and you'll see success 
sooner when you try to come up with a solution yourself, present it here (or 
on the tutor mailing list) and let us fill the gaps or suggest improvements.





































>>> import csv
>>> with open("fruit.csv") as f:
...     lookup = {k: float(v) for k, v in csv.reader(f)}
... 
>>> lookup
{'orange': 3.0, 'apple': 3.5, 'banana': 4.0}
>>> 2*lookup["banana"] + 3*lookup["orange"]
17.0

[toc] | [prev] | [next] | [standalone]


#107656

Fromjustin walters <walters.justin01@gmail.com>
Date2016-04-26 07:51 -0700
Message-ID<mailman.110.1461682290.32212.python-list@python.org>
In reply to#107652
On Tue, Apr 26, 2016 at 7:18 AM, +dime+ <donald.ng@gmail.com> wrote:

> I am learning python.
>
> if I have a csv file, like this
> banana,4.0
> apple,3.5
> orange,3.0
>
> Can anyone show me how to read the csv file line by line and then create a
> dictionary to contain these keys and values?
>
>
> Regards,
> +dime+
> --
> https://mail.python.org/mailman/listinfo/python-list
>

It's best if you look up how to do this in the docs yourself. An even
better way would be to think about the steps necessary to do this and then
writing them down in pseudo-code. After that you can look up the
functions/tools that will help you accomplish this task. One of the most
important skills a programmer can have is called 'algorithmic thinking'.
That is, essentially, the ability to break a problem down into smaller
pieces in order to eventually solve it.


So, think about this:

What steps do you need to take to turn this csv file into a Python
dictionary object? Imagine that you were going to do this by hand. How
would you do it?

[toc] | [prev] | [next] | [standalone]


#107668

FromTim Chase <python.list@tim.thechases.com>
Date2016-04-26 09:36 -0500
Message-ID<mailman.115.1461688268.32212.python-list@python.org>
In reply to#107652
On 2016-04-26 07:18, +dime+ wrote:
> I am learning python.
> 
> if I have a csv file, like this
> banana,4.0
> apple,3.5
> orange,3.0
> 
> Can anyone show me how to read the csv file line by line and then
> create a dictionary to contain these keys and values?

import csv
with open('data.csv') as f:
  data = dict(csv.reader(f))

Hard to get much more straight-forward.

-tkc

[toc] | [prev] | [next] | [standalone]


#107695

FromHasan Diwan <hasandiwan+usenet@gmail.com>
Date2016-04-27 00:36 +0000
Message-ID<nfp1ht$m8q$1@gioia.aioe.org>
In reply to#107668
>> I am learning python.
>> 
>> if I have a csv file, like this
>> banana,4.0
>> apple,3.5
>> orange,3.0
>> 
>> Can anyone show me how to read the csv file line by line and then
>> create a dictionary to contain these keys and values?

with open('data.csv') as f: 
    data = dict([[l.strip() for l in line] for line in csv.reader(f)])

data
 {'apple': '3.5', 'banana': '4.0', 'orange': '3.0'}

[toc] | [prev] | [next] | [standalone]


#107699

Fromjfong@ms4.hinet.net
Date2016-04-26 19:42 -0700
Message-ID<eb424848-fcb2-4973-b48f-be1a849cfc58@googlegroups.com>
In reply to#107652
Just curious:-) why everyone here open the csv file without using newline='' as suggested in Python 3.4.4 document section 14.1?

[toc] | [prev] | [next] | [standalone]


#107714

FromSibylle Koczian <nulla.epistola@web.de>
Date2016-04-27 11:10 +0200
Message-ID<mailman.144.1461748232.32212.python-list@python.org>
In reply to#107699
Am 27.04.2016 um 04:42 schrieb jfong@ms4.hinet.net:
> Just curious:-) why everyone here open the csv file without using newline='' as suggested in Python 3.4.4 document section 14.1?
>
And if the csv module is used anyway, why not simply read into a DictReader?

[toc] | [prev] | [next] | [standalone]


#107716

FromPeter Otten <__peter__@web.de>
Date2016-04-27 11:31 +0200
Message-ID<mailman.145.1461749491.32212.python-list@python.org>
In reply to#107699
Sibylle Koczian wrote:

> Am 27.04.2016 um 04:42 schrieb jfong@ms4.hinet.net:
>> Just curious:-) why everyone here open the csv file without using
>> newline='' as suggested in Python 3.4.4 document section 14.1?

Carelessness, lack of knowledge (I plead guilty), not on Windows and no 
embedded newline in sight?

> And if the csv module is used anyway, why not simply read into a
> DictReader?

How would that help with looking up 3.5 by "apple" given the OP's sample 
data

banana,4.0
apple,3.5
orange,3.0

? Please give a code example.

Finally, why would you think that asking rhetorical questions is a good way 
to communicate?

[toc] | [prev] | [next] | [standalone]


#107728

FromSibylle Koczian <nulla.epistola@web.de>
Date2016-04-27 17:37 +0200
Message-ID<mailman.151.1461771461.32212.python-list@python.org>
In reply to#107699
Am 27.04.2016 um 11:31 schrieb Peter Otten:
> Sibylle Koczian wrote:
>
>> And if the csv module is used anyway, why not simply read into a
>> DictReader?
>
> How would that help with looking up 3.5 by "apple" given the OP's sample
> data
>
> banana,4.0
> apple,3.5
> orange,3.0
>

Quite right, it wouldn't. Misread the problem, sorry.


[toc] | [prev] | [next] | [standalone]


#107730

FromSibylle Koczian <nulla.epistola@web.de>
Date2016-04-27 17:37 +0200
Message-ID<mailman.152.1461773405.32212.python-list@python.org>
In reply to#107699
Am 27.04.2016 um 11:31 schrieb Peter Otten:
> Sibylle Koczian wrote:
>
>> And if the csv module is used anyway, why not simply read into a
>> DictReader?
>
> How would that help with looking up 3.5 by "apple" given the OP's sample
> data
>
> banana,4.0
> apple,3.5
> orange,3.0
>

Quite right, it wouldn't. Misread the problem, sorry.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web