Home > database >  Alternative to making a dictionary from text document
Alternative to making a dictionary from text document

Time:07-23

I took word pairs from a text file and made a dictionary:

x = open('sustantivos.txt', 'r') ## opens file and assigns it to a variable
y = x.read() ## reads open file object and assigns it to variable y
y = str(y).lower().replace(":", "") ## turns open file object into a string, then makes it lower case and replaces ":" with whitespace
z = y.splitlines() # make a list with each element being a word pair string, then assign to variable z

bank = {}

for pair in z: #go through every word pair string
    (key, value) = pair.split() #split the word pair string making a list with two elements, assign these to variable key and value
    bank[key] = value #add key value pair

x.close()

For reference this is an excerpt from the text file:

Amour: amor

Anglais: inglés

Argent: dinero

Bateau: barco

My question is: Is there are more efficient or different approach that you would do differently? Also I was curious if my understanding that I include in the comments is correct. Thanks in advance.

CodePudding user response:

Your inline notes are accurate except that line number 2 is where the opened file is read and its contents are turned into a string. Your use of str(y) in the third line is unnecessary and could simply be written as y.lower()...

Your parsing strategy is sound as long as you know that the file will always contain lines of key:value pairs on each and every line. However, There are a couple of recommendation I would make.

  1. Use a with statement when opening files. this avoids errors that can occur if the file isn't closed properly
  2. Don't read the whole file in at once.
  3. dict.update will take an iterable of length 2 as an argument

Using those tips your code can be rewritten as:

bank = {}
with open('sustantivos.txt', 'r') as x:
    for line in x:
        key, value = line.strip().split(':')
        bank[key] = value
        # bank.update([line.strip().split(':')]) <- or this
  • Related