i have this piece of code which only prints the line number of the incorrect words. i want it to print the linenumbers of the incorrect words from the txt file. Am i able to modify this code to do that?
# text1 is my incorrect words
# words is my text file where my incorrect word are in
from collections import defaultdict
d = defaultdict(list)
for lineno, word in enumerate(text1):
d[word].append(lineno)
print(d)
ive now done this but this prints the character its located like the place of the word rather then the line. this is the code
import sys
import string
text = []
infile = open(sys.argv[1], 'r').read()
for punct in string.punctuation:
infile = infile.replace(punct, "")
text = infile.split()
dict = open(sys.argv[2], 'r').read()
dictset = []
dictset = dict.split()
words = []
words = list(set(text) - set(dictset))
words = [text.lower() for text in words]
words.sort()
def allwords(line):
return line.split()
def iswrong(word):
return word in words
for i, line in enumerate(text):
for word in allwords(line):
if iswrong(word):
print(word, i))
the output of that code is
millwal 342