Python Forum
Need help using pathlib to read text file into dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help using pathlib to read text file into dictionary
#1
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
Reply
#2
(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().
Reply
#3
(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.
Reply
#4
You can pickle the dictionary itself using load and dump https://wiki.python.org/moin/UsingPickle
Reply
#5
Thanks everyone, got it working with first suggestion. Forgot the KISS principle, Keep It Simple Stupid.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recommended way to read/create PDF file? Winfried 3 2,901 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,469 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,129 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,125 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,297 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 6,687 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,205 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,624 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,972 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,072 Jan-21-2023, 11:33 PM
Last Post: SamLiu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020