Class 12 Python Text File Questions
Class 12 Python Text File Questions
To implement this function, read the file line by line, ensure each line is evaluated for the condition that the first and last letter are identical (ignoring punctuation), and if true, count the line. Simultaneously calculate each word's length and keep a running total to determine the average. Use string methods to remove any periods affecting the first and last characters. The code for this looks like: `def Count_CAR(): # open file, evaluate condition per line, # accumulate word sizes for avg calculation` .
To swap the first and last letters of each word, read the file line by line, split each line into words, and for each word, check if it has more than one character to perform the swap. Once the swap is performed, reconstruct the line and print it. The key consideration is maintaining word boundaries and not affecting punctuation. Implement this by replacing each word after transforming it: `def countmy(): f=open('DATA.txt','r') s=f.readlines() for i in s: word=i.split() for j in word: i=i.replace(j,j[-1]+j[1:-1]+j[0]) print(i) f.close()` .
Read the text file line by line, split it into words, and check each word for the presence of vowels using a regular expression or set membership method. Count each word that contains one or more vowels. This requires handling various cases and ensuring efficiency in search operations. An implementation example could be like: `def NO_VOWELS_WORDS(): # open file, loop through words and check for vowels using set or regex` .
To achieve this, read each line, check if it starts with a consonant by excluding vowels, identify the first vowel in the line, then swap it with the first character. It requires iterating over the characters of the line and conducting swaps after establishing conditions for consonants and vowels. The idea is demonstrated with: `def COUNTLINES(): # implementation details to check and swap # not provided but involves iterating and swapping as needed` .
Read the file and split its content into individual words, then use a conditional check to count how many words contain digits. Care needs to be taken to account for varying positions and multiple digits within words. Challenges include handling punctuation and ensuring words like '1st' are distinctly counted as containing digits. An example methodology: `def Count_words(): # details of reading and checking digits # focus on looping over words in lines` .
To implement this function, you can read the file line by line, count the occurrences of the word 'is' in each line, and keep track of the line with the maximum count. This can be achieved using a dictionary to store the count of 'is' for each line and then finding the maximum value in the dictionary. The code from the document provides a structure like: `def IS_CNT(): d={} f=open('STORY.txt','r') s=f.readlines() for i in s: d[i]=i.count('is') print(max(d, key=d.get)) IS_CNT()`. Ensure that the count method checks for the whole word to avoid partial matching like 'this' .
You need to read the file line by line, then split each line into words. For each word that starts with 'I', reverse the word and replace it in the line. Write the modified lines into a new file. The implementation involves opening the files, iterating through each line and word, reversing the words starting with 'I', and writing the changes to the new file: `def REV_TEXT(): f=open('Content.txt','r') f1=open('New.txt','w') s=f.readlines() for i in s: word=i.split() for j in word: if j[0]=='I': i=i.replace(j,j[::-1]) f1.write(i) f.close() f1.close()` .
Read the file and split lines into words, then apply conditions to check if each word has more than seven characters or if it ends with 's'. Use Python's string functions for these checks and increment a counter if the criteria are met. Ensure robustness when handling different cases of 'S'. The function may appear as: `def COUNTWORD(): # read file, iterate over words, apply conditions, # count and report` .
The process involves reading the entire file, splitting it into lines and then words. For every word longer than a single character, swap the first and last characters. Implementing this requires careful handling of word boundaries and ensuring punctuation is not incorrectly altered. Pitfalls include managing edge cases like single-character words or punctuation affecting swaps. The method resembles: `def countmy(): # read input, iteratively swap letters, preserve word and punctuation integrity` .
To reverse each word starting with 'I', read the file and split it by space or punctuation, iterate through the list of words, apply a reverse operation on those starting with 'I', and construct the modified version to print. It's crucial to isolate each word correctly and apply reversal efficiently. A Python code structure like: `def RevText(): # read file and identify words, reverse if starts with I, then print` .


