Python Forum
reading a file int a dictionary specifically
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading a file int a dictionary specifically
#1
Im relatively new to python and I have a file containing words/lines like this:


AA EY2 EY1
AAA T R IH2 P AH0 L EY1
AABERG AA1 B ER0 G
AACHEN AA1 K AH0 N
AACHENER AA1 K AH0 N ER0
AAH AA1
AAKER AA1 K ER0
AALIYAH AA2 L IY1 AA2
AALSETH AA1 L S EH0 TH
AAMODT AA1 M AH0 T
AANCOR AA1 N K AO2 R

what i need to do read these into a dictionary with the key being the first word/entry of each line, and that is mapped to a list containing each string. for example- AA: "EY2", "EY1"
that way i can search for two words (indexes of the dictionary) and compare their lists. I've got a basic loop to read the file line by line stripping the newline character but not sure how to precede.

 
    d= {}
    with open(fName) as f:
        while True:
            line = f.readline()
            if not line.startswith(";;;"):
                break
        for line in f:
            print(line, end="")
i feel its important to recognize that after each of the first words in every line theres a double space. not just a single space. which has me a little tripped up since my first instinct was to use .split
Reply
#2
Eliminate everything under the while True (what is this supposed to do). Print anything in the code below that you do not understand.
d= {}
with open(fName) as f:
    for line in f:
        print(line, end="")
        line_as_list=line.strip().split()
        this_key=line_as_list[0]  ## easier to understand this way??
        d[this_key]=[]
        for item in line_as_list[1:]  ## skip key/first item
            d[this_key].append(item) 
Reply
#3
that first part is because theres a comment in the beginning of the file and that works around it.
Reply
#4
Then "for line in f" will read the next record, skipping over the while True record, no matter what the while True record contains. Just add a test
d= {}
with open(fName) as f:
    for line in f:
        print(line, end="")
        if not line.strip().startswith(";"):
            line_as_list=line.strip().split()
            this_key=line_as_list[0]  ## easier to understand this way??
            d[this_key]=[]
            for item in line_as_list[1:]  ## skip key/first item
                d[this_key].append(item) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 643 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 918 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,108 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,100 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 990 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 899 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 992 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,832 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading .csv file doug2019 4 1,706 Apr-29-2022, 09:55 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,254 Mar-27-2022, 02:08 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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