Reading Lines From a Text File Having Characters Greater or Equal to 50.

Q. Write a program to read lines from a text file having characters greater than or equal to 50.

file: text.txt
count-lines-from-text-file-in-python

Solution:

# print only those lines which are greater or equal to 50 characters
def read_data():
    f = open("text.txt", 'r')
    s = f.readlines()
    for i in s:
        if len(i) >= 50:
            print(i)
        else:
            pass


read_data()

Output:

#I would love to try or hear the sample audio your app can produce. 

#I do not want to purchase, because I've purchased so many apps that say they do something and do not deliver.  

#Can you please add audio samples with text you've converted? I'd love to see the end results.

how-to-count-lines-in-python

Explanation: Here we have defined a function read_data(). Inside read_data() function we have also created a file object “f” and opened our text file in read mode ie. “r” mode.

In next step, we have initialized all the values to variable “s“. And used readlines() function to get read our file line by line. The readlines() function return the output in list format.

So, now we have the complete text data in list format and we can slice each line using list indexing method. We implemented for loop() to iterate over each element of list which is the each lines of our text files stored in list format.

if len(i) >= 50:

Here inside for loop, if condition checks for the the each line length with len() function. If length of any line greater than or equal to 50, then it will return that line and print as output else: line will be skipped.

  1. Programming questions on Text Files

    1. WAP to define a method to read text document line by line.
    2. WAP to define a method in python to read lines from a text file starting with an alphabet F.
    3. WAP to define a method to count number of lines starting with an alphabet F.
    4. WAP to define a method which display only those lines starting with an alphabet A or F.
    5. WAP to define a method to count total number of characters in our text file.
    6. WAP to define a method to count total numbers of word available in our text file.
    7. WAP to define a method which counts the occurrence of particular word in a text file.
    8. WAP to define a method which only print the words having more than 5 characters.
    9. WAP to define a method which counts the occurrence of “is”, “to” in a text file.

    Programming questions on Binary Files

    1. WAP to define a method which displays the records of student having marks between 50 & 70.
    2. WAP to define a method which displays only those student records who secured grade A.