Python Forum

Full Version: Need help using pathlib to read text file into dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First, this is not homework - it's a program I am writing for my wife's pet sitting business. Basically, it's a calendar program where I can add or remove pets that will be boarding with us. All of the basic functions work - adding pets, removing pets, etc. The problem I am having is the read and write functions using pathlib and a text file named boarders with a dictionary.

The structure of the dictionary is: boarders[brdr_year][brdr_month][brdr_day]{pet_name}

This is what I have written so far for to retrieve the contents of the file and put in the dictionary but honestly have no idea of how to proceed:

def open_file():
    data_folder = Path.cwd()
    print(data_folder)
    path = data_folder / "boarders.txt"
    print(path)
    if str(path.exists()):
        print("File Does Exist")
        print(boarders)
    else:
        print("File Doesn't Exist")
Any help would be appreciated greatly. Big Grin Thanks
(Aug-13-2018, 04:14 PM)gwilli3 Wrote: [ -> ]The structure of the dictionary is: boarders[brdr_year][brdr_month][brdr_day]{pet_name}

I am a bit confused. So when a client come in and checks in a pet. You want to manually add the pets info: brdr_year, brdr_month, brdr_day and the pets name to a text file? Then, given that text file information you want to generate a dictionary called "boarders" so that you can update some kind of calendar script you have built?

In general, I would suggest not over-complicating the issue with pathlib. Also, are you on a Windows PC or a Linux based distro?

In general, to keep things simple keep the text file in the same directory as the script then:
file = "output.txt"

with open(file, 'r') as input:
  content = input.read()

print(content)
It is important to keep in mind (deppending on your needs) there are multiple ways of reading in lines:

output for read():
Output:
This is the content of the first sentence. This is the content of the second sentence. This is the content of the second line.
As you can see this gives the entirety of the text files contents as one long string.

output for readline():
Output:
This is the content of the first sentence. This is the content of the second sentence.
Similar to read() you are returned a string but it goes line by line.

output for readlines():
Output:
['This is the content of the first sentence. This is the content of the second sentence.\n', 'This is the content of the second line.\n']
Similar to read() in that it gives you the entirety of the text files content; however, now you are returned a list.

That being said, when it comes to converting these contents into dictionaries the obvious choice for which to use in my mind would be readlines().
(Aug-13-2018, 05:36 PM)Vysero Wrote: [ -> ]
(Aug-13-2018, 04:14 PM)gwilli3 Wrote: [ -> ]The structure of the dictionary is: boarders[brdr_year][brdr_month][brdr_day]{pet_name}
I am a bit confused. So when a client come in and checks in a pet. You want to manually add the pets info: brdr_year, brdr_month, brdr_day and the pets name to a text file? Then, given that text file information you want to generate a dictionary called "boarders" so that you can update some kind of calendar script you have built? In general, I would suggest not over-complicating the issue with pathlib. Also, are you on a Windows PC or a Linux based distro? In general, to keep things simple keep the text file in the same directory as the script then:
file = "output.txt" with open(file, 'r') as input: content = input.read() print(content)
It is important to keep in mind (deppending on your needs) there are multiple ways of reading in lines: output for read():
Output:
This is the content of the first sentence. This is the content of the second sentence. This is the content of the second line.
As you can see this gives the entirety of the text files contents as one long string. output for readline():
Output:
This is the content of the first sentence. This is the content of the second sentence.
Similar to read() you are returned a string but it goes line by line. output for readlines():
Output:
['This is the content of the first sentence. This is the content of the second sentence.\n', 'This is the content of the second line.\n']
Similar to read() in that it gives you the entirety of the text files content; however, now you are returned a list. That being said, when it comes to converting these contents into dictionaries the obvious choice for which to use in my mind would be readlines().


Actually, when working with the program when I add a pet to a certain day , for ex: Fido on 1st of Aug, the information is automatically entered into the dictionary. If I remove the pet from the day, it also updates the dictionary. What I want is to save the dictionary into a text file and to be able to retrieve the data from the file and put it back into the dictionary.
You can pickle the dictionary itself using load and dump https://wiki.python.org/moin/UsingPickle
Thanks everyone, got it working with first suggestion. Forgot the KISS principle, Keep It Simple Stupid.