Missing values from dictionary loading from txt file - python

When I print the dictionary it seems to omit over half of the words. All of the words are over 2 characters as well so should be placed into their own key in the dictionary but seemingly ignored. To test I compared a file with the exact same words as in the dictionary. 36 of the words were found whilst 45 where missing.
Words that are not present in the dictionary.
The dictionary with the keys being first two letters of the word.
d = {}
#Opens the dictionary file from variable.
with open(dictionary, 'r') as f:
#iterates through each line
for line in f:
line_data = f.readline().strip()
#Gets the first to letters of the word to act as key
first_two = line_data[0:2]
#Checks if line is empty
if line_data is None:
break
#Checks if key is already present in dictionary, appends word to list
if first_two in d.keys():
d[first_two].append(line_data)
#If key is not present create a new key and a list for the words.
elif first_two not in d.keys():
list = [line_data]
d[first_two] = list

The program as written skips every other line: for line in f already reads the lines from the file, so f.readline() is redundant and is eating half your lines. Try this substitution:
for line in f:
line_data = line.strip()
... (the rest as is)

Related

Random substitution

I have a txt file and a dictionary, where keys are adjectives, values are their synonyms. I need to replace the common adjectives from the dictionary which I meet in a given txt file with their synonyms - randomly! and save both versions - with changed and unchanged adjectives - line by line - in a new file(task3_edited_text). My code:
#get an English text as a additional input
filename_eng = sys.argv[2]
infile_eng = open(filename_eng, "r")
task3_edited_text = open("task3_edited_text.txt", "w")
#necessary for random choice
import random
#look for adjectives in English text
#line by line
for line in infile_eng:
task3_edited_text.write(line)
line_list = line.split()
#for each word in line
for word in line_list:
#if we find common adjectives, change them into synonym, randomly
if word in dict.keys(dictionary):
word.replace(word, str(random.choice(list(dictionary.values()))))
else:
pass
task3_edited_text.write(line)
Problem is in the output adjectives are not substituted by their values.
line_list = line.split()
...
task3_edited_text.write(line)
The issue is that you try to modify line_list, which you created from line. However, line_list is simply a list made from copying values generated from line ; modifying it doesn't change line in the slightest. So writing line to the file writes the unmodified line to the file, and doesn't take your changes into account.
You probably want to generate a line_to_write from line_list, and writing it to the file instead, like so:
line_to_write = " ".join(line_list)
task3_edited_text.write(line_to_write)
Also, line_list isn't even modified in your code as word is a copy of an element in line_list and not a reference to the original. Moreover, replace returns a copy of a string and doesn't modify the string you call it on. You probably want to modify line_list via the index of the elements like so:
for idx, word in enumerate(line_list):
#if we find common adjectives, change them into synonym, randomly
if word in dict.keys(dictionary):
line_list[idx] = word.replace(word, str(random.choice(list(dictionary.values()))))
else:
pass

How to write a program that reads a .txt file that contains words with numbers in a separate column and print out the word with the biggest number?

I am very new to coding so this is all I have right now. I am running into problems trying to select the highest number and printing out the word that corresponds with it.
Edit: I am also supposed to write a code that also ignores the '#'character and anything that comes after it.
text = open('heroes.txt', 'r')
for line in sorted(text, key = lambda line: line.split()[1]):
if not '#' in line:
line.isalnum() or line == '_'
continue
This is the text file with the lists
Based off SuperStew's answer, changed to make it a little more intuitive for a beginner. Added comments.
text = open('heroes.txt', 'r')
# Empty dictionary
data = {}
# For every line in the file...
for line in text.readlines():
# skip if line starts with #
if line.startswith('#'):
continue
# Split the name into two parts, hero name and hero power.
row = line.split()
# Map row data to dictionary. Hero name will be key, hero power will be value.
data[row[0]] = row[1]
# Remember to close the file
text.close()
# For every key value pair in data...
for k,v in data.items():
# See if value is the max value. If so, print it.
if v==max(data.values()):
print(k)
Useful links:
https://www.w3schools.com/python/ref_string_split.asp
https://www.w3schools.com/python/python_dictionaries.asp
this might do the trick
text = open('heroes.txt', 'r')
data={}
for line in text.readlines():
if line.startswith('#'):
continue
d_l=line.split()
d_l=[[x.strip() for x in y] for y in d_l]
data[d_l[0]]= d_l[1]
text.close() #remember to close file
for k,v in data.items():
if v==max(data.values()):
print(k)

Need to copy the contents of a text file to a dictionary

I have a text file such that each line consists of one word followed by a comma-separated list of that word's synonyms. So for example, one line would look like this:
word, synonym1, synonym2, synonym3
so the first word in each line is the key and the rest are its values
Solution
with open('file_name.txt') as fobj:
synonyms = {}
for line in fobj:
key, *values = [entry.strip() for entry in line.split(',')]
synonyms[key] = values
produces this dictionary synonyms:
{'word1': ['synonym11', 'synonym12', 'synonym13'],
'word2': ['synonym21', 'synonym22', 'synonym23']}
for this file content:
word1, synonym11, synonym12, synonym13
word2, synonym21, synonym22, synonym23
Explanation
Open the file using with open('file_name.txt') as fobj: This opens the file with the promise to close it after dedenting.
Make a new empty dictionary: synonyms = {}.
Go through all lines for line in fobj:.
Split each line at the comma and remove extra white space from each word: [entry.strip() for entry in line.split(',')].
Use the new *-way to unpack an iterable in Python 3 to split key and values key, *values =.
Add the values to the result synonyms[key] = values.
Addition:
Print word and a random synonym:
import random
for word, syns in synonyms.items():
print(word, random.choice(syns))
prints:
word1 synonym12
word2 synonym22

How to create a dictionary that contains key‐value pairs from a text file

I have a text file (one.txt) that contains an arbitrary number of key‐value pairs (where the key and value are separated by a colon – e.g., x:17). Here are some (minus the numbers):
mattis:turpis
Aliquam:adipiscing
nonummy:ligula
Duis:ultricies
nonummy:pretium
urna:dolor
odio:mauris
lectus:per
quam:ridiculus
tellus:nonummy
consequat:metus
I need to open the file and create a dictionary that contains all of the key‐value pairs.
So far I have opened the file with
file = []
with open('one.txt', 'r') as _:
for line in _:
line = line.strip()
if line:
file.append(line)
I opened it this way to get rid of new line characters and the last black line in the text file. I am given a list of the key-value pairs within python.
I am not sure how to create a dictionary with the list key-value pairs.
Everything I have tried gives me an error. Some say something along the lines of
ValueError: dictionary update sequence element #0 has length 1; 2 is required
Use str.split():
with open('one.txt') as f:
d = dict(l.strip().split(':') for l in f)
split() will allow you to specify the separator : to separate the key and value into separate strings. Then you can use them to populate a dictionary, for example: mydict
mydict = {}
with open('one.txt', 'r') as _:
for line in _:
line = line.strip()
if line:
key, value = line.split(':')
mydict[key] = value
print mydict
output:
{'mattis': 'turpis', 'lectus': 'per', 'tellus': 'nonummy', 'quam': 'ridiculus', 'Duis': 'ultricies', 'consequat': 'metus', 'nonummy': 'pretium', 'odio': 'mauris', 'urna': 'dolor', 'Aliquam': 'adipiscing'}

Python for loop not iterating

I'm trying to loop through a list of strings and add them to a dictionary if their length equals a length input by the user. When the last loop runs, it only runs one time. I know this because the first word in the dictionary is 8 characters long, and when the user input is 8, it prints just that word, and not the other 8 character words. If the input is 3, an empty dictionary is printed. Why is my loop not iterating through all of the words in the list linelist?
wordLength = raw_input("Enter a word length ")
word_dict = {}
infile = open("dictionary.txt")
for line in infile:
line = line.strip()
linelist = line.split(" ")
for word in linelist:
if len(word) == int(wordLength):
if len(word) in word_dict:
word_dict[len(word)] = word_dict[len(word)].append(word)
else:
word_dict[len(word)] = word
print word_dict
Each time your first loop runs, it sets linelist to a new value, overwriting any old value. After that first loop runs, linelist will contain only the split result from the last line of the file. Every time you process one line of the file, you are throwing away whatever you did with the previous line.
If you want to build a list of all words in the dictionary file, you need to make a list and append to it on each iteration of your for line in infile loop.
Also, it doesn't make much sense to use split on each line if each line is just one word, since there will be no splitting to be done.
for line in infile:
line = line.strip()
linelist = line.split(" ")
Every time you do linelist = line.split(" "), that replaces the old linelist with words from just the last line. The list ends up only holding words from the last line. If you want words from the entire file, create a single linelist and extend it with new words:
linelist = []
for line in infile:
# split with no argument splits on any run of whitespace, trimming
# leading and trailing whitespace
linelist += line.split()
# ^ this means add the elements of line.split() to linelist
Since apparently every word is on its own line, though, you shouldn't even be using split:
words = [line.strip() for line in infile]
Your second loop is not indented, so you run it only on the last value of linelist.

Categories