So I figured out how to make my text file into a dictionary but there is problem that is occurring. This is an example text file:
pig: animal
dog: animal
cat:animal
car: thing
When I convert this into a dictionary I get: {" animal": ['pig','dog'], "animal" : ["cat"], " thing" : ['car']}
I need the cat to go with the pig and dog: {" animal": ["pig', 'dog', 'cat']} but since there is no space after the colon in the text file my program is not recognizing the key is still the same.
Thank you, any help is needed!
CodePudding user response:
You can remove all space after ':'. So, every value will have no space after column(:). For example:
with open('test.txt', mode='r') as file:
text = file.read()
clean_text = re.sub(r':( ) ', ':', text)
with open('output.txt', 'w') as file_out:
file_out.write(clean_text)
so for Input:
pig: animal
dog: animal
cat:animal
car: thing
Output:
pig:animal
dog:animal
cat:animal
car:thing
CodePudding user response:
You could process the file and fix the colon space problem:
file = open('your_text.txt', mode='r')
text = file.read()
text = re.sub(r':(\w)', r': \1', text)
file.close()
with open('your_text.txt', 'w') as file_out:
file_out.write(text)
CodePudding user response:
You may also try this by using the strip
method if you do not wish to create a new output file.
a = {}
with open("text.txt") as file:
for line in file.readlines():
text = line.partition(":")
value, key = text[0].strip(), text[2].strip()
a.setdefault(key, [])
a[key] = [value]
print(a)
CodePudding user response:
One of the appraches:
from collections import defaultdict
data = ['pig: animal', 'dog: animal', 'cat:animal', 'car: thing']
out = defaultdict(list)
for entry in data:
entry_data = entry.split(':')
out[entry_data[1].strip()].append(entry_data[0].strip())
print (out)
Output:
defaultdict(<class 'list'>, {'animal': ['pig', 'dog', 'cat'], 'thing': ['car']})