Reputation: 87
I write a python code wanting to read a text file and user can put in the word and it will print out the desire words from the text file.
Example of the text file is like this:
u:you
ttyl:talk to you later
l8:late
brb:be right back
lol:laughing out loud
bbl:be back later
tldr:too long; didn't read
rofl:rolling on floor laughing
gtg:got to go
This is my code so far:
dictionary = {}
file = open('abbreviations.txt', 'r')
lines = file.readlines()
count = 1
for line in lines:
data = line.split(":")
dictionary[data[0]] = data[1]
print("Line{}: {}".format(count, line.strip()))
count += 1
word = input("Please enter an abbreviations: ")
if dictionary.has_key(word):
print(dictionary[word])
else:
print(word)
And when I run it, it shows error in line 12 like this:
AttributeError: 'dict' object has no attribute 'has_key'
And this is my desire output:
Please enter an abbreviations: u gtg
output: u got to go
Upvotes: 2
Views: 153
Reputation: 670
To add to the previous answer, as your input is actually several words, u
and gtg
, you need to split the input string and check each token. Something like:
words = input("Please enter an abbreviations: ")
out = []
for word in words.split():
w = dictionary.get(word, word) # Get word if in dict, or just return the word if not
out.append(w)
' '.join(out)
then an input of u gtg
returns:
'you got to go'
Upvotes: 0
Reputation: 3455
This little example seems to work
abr_file = 'abbreviations.txt'
with open(abr_file, 'rt') as f:
lines = f.readlines()
abbr_dict = {}
for line in lines:
k, v = [v.strip() for v in line.strip('\n').split(':')]
abbr_dict[k] = v
while True:
sentence = input('Please give me a sentence with abbreviations: ')
words = [w.strip() for w in sentence.split()]
full_words = []
for word in words:
if word in abbr_dict:
full_words.append(abbr_dict[word])
else:
full_words.append(word)
full_sentence = ' '.join(full_words)
print(full_sentence)
Please give me a sentence with abbreviations: u gtg
you got to go
Please give me a sentence with abbreviations:
You just need to fix it for punctuation.
Upvotes: 0
Reputation: 27311
When reading a file, you should use the with
statement:
dictionary = {}
with open('abbreviations.txt') as fp:
for line in fp: # loop over lines in file
word, explanation = line.split(':', 1) # only split on the first :
dictionary[word] = explanation.strip() # remove final newline
if you want to look at the dictionary, uncomment the following lines:
# import json
# print(json.dumps(dictionary, indent=4))
Your description didn't really match your code, so I followed your description i.e. expand all the dictionary words:
words = input("Please enter an abbreviations: ")
for word in words.split(): # split input into individual words
print(dictionary.get(word, word), end=' ')
print() # final newline
dictionary.get('a', 'b')
will return dictionary['a']
if it exists in the dictionary
, otherwise it returns the string 'b'
. Above I use it to return the lookup word itself if it is not in the dictionary. The print
function normally prints a newline at the end, but we want to keep the text on a single line so I tell it to print a space at the end instead. To make things look pretty I print a newline after all the words are printed (otherwise your prompt would end up after the last character).
Output:
Please enter an abbreviations: u gtg
you got to go
Aside: it's not normally a good idea to create your own file format for storing data if you can use an existing one.
If you change abbreviations.txt
to (i.e. just adding a space after the colon):
u: you
ttyl: talk to you later
l8: late
brb: be right back
lol: laughing out loud
bbl: be back later
tldr: too long; didn't read
rofl: rolling on floor laughing
gtg: got to go
makes it valid YAML and you can use a yaml library to read the file instead. I like the ruamel.yaml package (but there are others).
Then you can create dictionary
by:
from ruamel.yaml import YAML
yaml = YAML(typ='safe')
dictionary = {}
with open('abbreviations.txt') as fp: # see note below
dictionary = yaml.load(fp)
note: renaming the file to abbreviations.yaml
will give you syntax highlighting etc. in most editors.
Upvotes: 1
Reputation: 4854
has_key()
is deprecated and has been removed in Python 3. To check for membership, use the in
operator:
if word in dictionary:
print(dictionary[word])
else:
print(word)
If you have multiple words in an input separated by space, like u gtg
, you need to split them first. Then these two lines are all you need:
words = input("Please enter the abbreviations: ").split();
print(" ".join(map(lambda word: dictionary[word] if word in dictionary else word, words)))
Here, the words in the input will be split by space and stored in words
. Next, I'm using the map()
function to create another sequence from words
where each abbreviation in words
will be replaced by its value in the dictionary. The map()
function will iterate through each word in words
and call the lambda word: dictionary[key] if word in dictionary else word
for each word. The result will be a new sequence with all abbreviations removed. Each word in the new sequence will be joined by a space using ' '.join()
.
Finally, you must close the file using file.close()
to release the file. Or as @thebjorn mentioned, using a context manager (with
) is a much better option.
Upvotes: 0
Reputation:
Replace if dictionary.has_key(word):
with if dictionary.get(word):
if dictionary.get(word):
print(dictionary[word])
else:
print(word)
Upvotes: 0