Import pprint message= input(‘enter the file name’) with open(filename, ‘r’) as file:text=file.read() words = text.split() print (‘the words are’) pprint. pprint (words) freq = { } for word in words: freq[word]= freq.get (word,0) + 1 print (‘the frequency of word is’) print (freq) sorteddict = sorted ([(value, key) for key,value in freq.items()], reverse=True) print (‘the ten most frequently appeared words in the file is’) pprint.pprint (sorteddict [0:10] )
It looks like you're trying to create a program to analyze the frequency of words in a text file. However, there seem to be a couple of issues in the code you provided.
Firstly, it seems like there might be a typo the variable name. In the first line, you are assigning user input to the variable 'message' but then using 'filename' in the subsequent lines. It might be better to use the variable name throughout.
Additionally, when opening the file, you are using a variable 'filename' which is defined. You should use the variable obtained from the user inputHere's a corrected version of your code:
import pprint filename = input('Enter the file name: ') with open(filename, 'r') as file: text = file() words = text.split() print('The words are:') pprint.pprint) freq = {} for word in words: freq[word] freq.get(word, 0) + 1 printThe frequency of each word is:') print(freq) sorteddict = sorted([(, key) for key, value in freq.items()], reverse) print('The ten most frequently appeared words in the file are:') pprint.pprint(sorteddict[:])
This corrected version should help you analyze the frequency of words in a text file. If you encounter any further issues or have additional questions, feel free to ask!
infile=input("Enter") outfile=input("enter") f1=open(infile,'r') content=f1.readlines() f1.close() content.sort()
f2=open(outfile,'r') for line in content: f2.write(line)
f2.close()
It seems like you're trying to read the contents of one file sort the lines, and then write them into another file. However, there are a few issues in the code you provided. Here's a corrected version of your code:
infile = input("Enter the input file name: ") outfile = input("Enter the output file name: ") with open(infile, 'r') as f1: content f1.readlines() content.sort() with open(outfile, 'w') as f: for line in content: f2.write(line)
In the corrected version, I've made a few adjustments to properly open and close the files using the 'with' statement and corrected the writing process to the output file. This should help you read from one file, sort the content, and write the sorted content another file. If you have any other questions or need further assistance, feel free to ask!