Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Files in Python:
Python, like many other programming languages, offers file handling and allows users to read and write files, as well as perform a variety of other file-related tasks. The concept of file handling has been extended to a variety of other languages, but the implementation is either complicated or lengthy. However, like most Python principles, this concept is simple and straightforward. Python processes file differently depending on whether they are text or binary, which is crucial. Each line of code consists of a series of characters that together constitute a text file. A specific character called the EOL or End of Line character, such as the comma, or a newline character is used to end each line in a file.
Given a file, the task is to read the given text file and print all the numbers present in the given text file.
Program to Read a Text File and Print all the Numbers Present in the Text File
Below is the full process to read a text file and print all the numbers present in the given text file.
- Create the sample file or upload the existing file.
- In read mode, open the first file say samplefile.txt.
- Using for loop, go over the lines in the sample file.
- Split the line into words using the split() function.
- Traverse through the words using for loop.
- Traverse through all the characters of the word using another For loop.
- Check to see if the letter is a digit, and if so, print it.
- Exit of program
Below is the implementation:
filename = input('Enter the name of the given file : ') # In read mode, open the given file with the name 'filename' with open(filename, 'r') as file: # Using for loop, go over the lines in the sample file. for line in file: # Split the line into words using the split() function. words = line.split() # Traverse through the words using for loop. for i in words: # Traverse through all the characters of the word using another for loop. for letter in i: # Check to see if the letter is a digit, and if so, print it. if(letter.isdigit()): print(letter)
Enter the name of the given file : samplefile.txt 1 1 2 3 9 4 5 6 7 9 8 3 3
- A file name must be entered by the user.
- In read mode, the file is opened with the open() method.
- To read each line in the file, a for loop is utilized.
- Using split, each line is divided into a list of words ().
- The word list is traversed using a for loop, and the letters of the word are traversed using another For loop.
- The digit is printed if the letter encountered is a digit.
- Here it prints all the digits present in the given file separately in a newline.
Sample file.txt :
hel11l2o th3is i9s B4TechGe5eks Onl6ine Cod7ing Platf9orm fo8r Bt3ech Stude3nts
Google Colab Images:
File and Code:
- Python Program to Read a File and Capitalize the First Letter of Every Word in the File
- Python Program to Append the Contents of One File to Another File
- Python Program to Read a Text File and Print all the Numbers Present in the Text File
- Python Program to Copy the Contents of One File into Another
- Python Program to Count the Number of Words in a Text File
- Python Program to Count the Number of Lines in a Text File
- Python Program to Count the Number of Blank Spaces in a Text File
- Python Program to Count the Occurrences of a Word in a Text File