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
  Replace values in Yaml file with value in dictionary PelleH 1 2,130 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 1,045 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Problem with pathlib ? Ota 3 1,320 Nov-11-2024, 06:04 AM
Last Post: MoMoProxy
  Read TXT file in Pandas and save to Parquet zinho 2 1,239 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,572 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,665 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,283 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,691 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,820 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,587 Jul-06-2023, 01:52 AM
Last Post: Tupa

Forum Jump:

User Panel Messages

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