Python Forum
How to convert Text file contents into a dictionary. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to convert Text file contents into a dictionary. (/thread-3084.html)

Pages: 1 2


How to convert Text file contents into a dictionary. - tannishpage - Apr-28-2017

I have a text file with contents like this:
    A:B
    C:D
and so on(Its just an example).

I want to put that into a dictionary. Is it possible? If so how do I do it.





RE: How to convert Text file contents into a dictionary. - ndc85430 - Apr-28-2017

Of course it is. Tell us what your ideas are. Do you know how to read files? Here's another thing to think about and to go investigate how to do: how do you separate the key from the value in the lines in your file?


RE: How to convert Text file contents into a dictionary. - tannishpage - Apr-28-2017

Ok well I have a good understanding of python. I am working on a project that sorts files and I need a place to store the extention:folderToPutIn
And working from text files is kind of time taking so I want to use dictionaries. I know how to open and do operations with files. (I have been pythoning for 1 year).
I sincerely  have no clue how to go about this.


RE: How to convert Text file contents into a dictionary. - buran - Apr-28-2017

(Apr-28-2017, 05:47 AM)tannishpage Wrote: (I have been pythoning for 1 year).
I sincerely have no clue how to go about this.

Do you see the contradiction in these two statements?


RE: How to convert Text file contents into a dictionary. - wavic - Apr-28-2017

Open the file and read it row by row. For each line do:

dict(zip(*iter(line.split(':'))))
This will return a dictionary.


RE: How to convert Text file contents into a dictionary. - tannishpage - Apr-28-2017

(Apr-28-2017, 06:09 AM)wavic Wrote: Open the file and read it row by row. For each line do:

dict(zip(*iter(line.split(':'))))
This will return a dictionary.

what does the zip do here? and why is it *iter?

(Apr-28-2017, 06:04 AM)buran Wrote:
(Apr-28-2017, 05:47 AM)tannishpage Wrote: (I have been pythoning for 1 year). I sincerely have no clue how to go about this.
Do you see the contradiction in these two statements?
I kinda do. Sadly although I've been doing python for 1 year, I still have lots to learn. If you can, recommend books, tutorials, or something that can help me learn more. Thank you :)


RE: How to convert Text file contents into a dictionary. - wavic - Apr-28-2017

You can remove iter().

dict(zip(*line.split(':')))



RE: How to convert Text file contents into a dictionary. - zivoni - Apr-28-2017

And you can remove zip and * too and use
dict([line.split(':')])
If dict() gets only one unnamed argument, it must be either mapping or  iterable containing pairs key, value. In your case line.split(": ") returns list of two items ("pair"), so it must be "put" into some iterable - it can be done by creating list or tuple containing that pair. Wavic's unpacking and zipping works for this specific keys/values, but try it for line AB:CD or A:BC.

key, value = line.split(":")
my_dict = {key: value}
is probably more beginner friendly and more readable way to create dict.

Anyway this will always create new dictionary for each line. If you want to have same dictionary with all values, you need to either "collect" all key: value pairs and then convert them into dict at once, or create dictionary and update it for each line. Second way:
my_dict = {}
with open('file.txt') as fileobj:
  for line in fileobj:
      key, value = line.split(":")
      my_dict[key] = value
Likely you would want to use .strip() to remove end of lines ("\n").


RE: How to convert Text file contents into a dictionary. - wavic - Apr-28-2017

Well, I was tried dict(list(s.split(':'))) but I've got:

Error:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
What is the difference between this and using square brackets?


RE: How to convert Text file contents into a dictionary. - zivoni - Apr-28-2017

list() used on iterable returns list containing items from that iterable, it does not return list containing given iterable. So when list() is used on list, it returns copy of given list. I guess thats for practical reasons - if it always returned list containing its argument, using list(range(5)) would give you list containing range generator instead of [0, 1, 2, 3, 4]. Same holds for tuple() and dict() behaves similarly.