Anay Bose Anay Bose - 4 months ago 20
Python Question

Output dictionary data dynamically from a python for loop

here, I am trying to spell check a txt file. first half of my code outputs original (var word) and corrected, if any, (var spell(word)). my replacement code uses a data structure like

{'zero':'0', 'temp':'bob', 'garbage':'nothing', 'garnvsh': 'garnish'}
. Now I would like to produce the same data structure from the loop dynamically. Can you, please, help me figure it out how to output similar data structure with
var word
var spell(word)
with first half of the code?

import os, os.path
from textblob import TextBlob
from autocorrect import spell
import fileinput
import json

data = []

with open("input.txt", "r") as my_file:
for line in my_file.readlines():
zen = TextBlob(line)
for word in zen.words:
word, spell(word)

replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing', 'garnvsh': 'garnish'}

with open('../input.txt') as infile, open('../output.txt', 'w') as outfile:
for line in infile:
for src, target in replacements.iteritems():
line = line.replace(src, target)


I suppose you want to create dictionary which its keys are word and values are spell(word).

my_dict = { word: spell(word) for word in zen.words }