Need help using pathlib to read text file into 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: Need help using pathlib to read text file into dictionary (/thread-12185.html) |
Need help using pathlib to read text file into dictionary - gwilli3 - Aug-13-2018 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. Thanks RE: Need help using pathlib to read text file into dictionary - Vysero - Aug-13-2018 (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(): As you can see this gives the entirety of the text files contents as one long string.output for readline(): Similar to read() you are returned a string but it goes line by line.output for readlines(): 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(). RE: Need help using pathlib to read text file into dictionary - gwilli3 - Aug-13-2018 (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():As you can see this gives the entirety of the text files contents as one long string. output for readline(): 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. RE: Need help using pathlib to read text file into dictionary - woooee - Aug-13-2018 You can pickle the dictionary itself using load and dump https://wiki.python.org/moin/UsingPickle RE: Need help using pathlib to read text file into dictionary - gwilli3 - Aug-13-2018 Thanks everyone, got it working with first suggestion. Forgot the KISS principle, Keep It Simple Stupid. |