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
#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


Messages In This Thread
RE: Need help using pathlib to read text file into dictionary - by Vysero - Aug-13-2018, 05:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 358 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 3,043 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,732 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,227 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,220 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,541 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,762 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read csv file with inconsistent delimiter gracenz 2 1,308 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,794 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 2,177 Jan-25-2023, 04:12 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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