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

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

Related

How to convert a file with words on different newlines into a dictionary based on values each word has?

I am trying to convert a file, where every word is on a different newline, into a dictionary where the keys are the word sizes and values are the lists of words.
The first part of my code has removed the newline characters from the text file, and now I am trying to organize the dictionary based on the values a word has.
with open(dictionary_file, 'r') as file:
wordlist = file.readlines()
print([k.rstrip('\n') for k in wordlist])
dictionary = {}
for line in file:
(key, val) = line.split()
dictionary[int(key)] = val
print(dictionary)
However, I keep getting the error that there aren't enough values to unpack, even though I'm sure I have already removed the newline characters from the original text file. Another error I get is that it will only print out the words in a dictionary without the newlines, however, they aren't organized by value. Any help would be appreciated, thanks! :)
(key, val) = line.split()
^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
I'm not sure why you're trying to use line.split(). All you need is the length of the word, so you can use the len() function. Also, you use collections.defaultdict to make this code shorter. Like this:
import collections
words = collections.defaultdict(list)
with open('test.txt') as file:
for line in file:
word = line.strip()
words[len(word)].append(word)
try this
with open(dictionary_file, 'r') as file:
dictionary = {}
for line in file:
val = line.strip().split()
dictionary[len(val)] = val
print(dictionary)

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

converting user details stored in a text file into a dictionary

I have tried converting the text file into a dictionary using the following code below:
d = {}
with open('staff.txt', 'r') as file:
for line in file:
(key, val) = line.split()
d[str(key)] = val
print(d)
The contents in the file staff.txt:
username1 jaynwauche
password1 juniornwauche123
e_mail1 juniornwauche#gmail.com
Fullname1 Junior Nwauche
Error: too many values to unpack
What am I doing wrong?
According to your file, the last line you have three words and you want to split them by space so you will have three words but just two variables.
You need to specify the split condition. Right now you are splitting each character, there for you get a list with a lot of elements. Try line.split(' ') like this:
d = {}
with open('staff.txt', 'r') as file:
for line in file:
(key, val) = line.split(' ')
d[str(key)] = val
print(d)
This will split the lines where there's an space, so you get only words on the list.

Missing values from dictionary loading from txt file

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)

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'}

Categories