Convert a text file content to a dictionary...
Meehan, Francois
Francois at iecholden.com
Tue Feb 4 11:55:14 EST 2003
Yeap that works. did also try the other proposed solutions.
The first element is indeed the key.
Got my solution, Thanks everyone, this group is first class!
Francois
>
> Hi all,
>
> I want to read a simple text file that has 2 columns (tab delimited), and
> put that content in a dictionary. What is the best way to achieve this???
>
> Regards,
>
> Francois
>
That depends on you what you mean by 'put the content in a dictionary'.
If you mean, as I see most likely, that you want the first column to be the
key, and the second the value, the below willt work.
import string
file = open('yourfile','r')
dict = {}
for l in file.readlines():
d = string.split(string.strip(l))
dict[d[0]] = d[1]
--
Tyler Eaves
Funadmental Python - Free Ebook!
http://cg1.org/pythontut.pdf
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list