{"id":9676,"date":"2021-09-30T14:00:45","date_gmt":"2021-09-30T08:30:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=9676"},"modified":"2021-11-22T18:34:35","modified_gmt":"2021-11-22T13:04:35","slug":"python-program-to-read-a-text-file-and-print-all-the-numbers-present-in-the-text-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-read-a-text-file-and-print-all-the-numbers-present-in-the-text-file\/","title":{"rendered":"Python Program to Read a Text File and Print all the Numbers Present in the Text File"},"content":{"rendered":"
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples<\/a> and try to code all of them on your own then check with the exact code provided by expert programmers.<\/p>\n Files in Python:<\/strong><\/p>\n 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.<\/p>\n Given a file, the task is to read the given text file and print all the numbers present in the given text file.<\/p>\n Below is the full process to read a text file and print all the numbers present in the given text file.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Sample file.txt :<\/strong><\/p>\n Google Colab Images:<\/strong><\/p>\n File and Code:<\/strong><\/p>\n <\/p>\n <\/p>\n Output Image:<\/strong><\/p>\n <\/p>\n <\/p>\n File Content:<\/strong><\/p>\n <\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nProgram to Read a Text File and Print all the Numbers Present in the Text File<\/h2>\n
\n
filename = input('Enter the name of the given file : ')\r\n# In read mode, open the given file with the name 'filename'\r\nwith open(filename, 'r') as file:\r\n # Using for loop, go over the lines in the sample file.\r\n for line in file:\r\n # Split the line into words using the split() function.\r\n words = line.split()\r\n # Traverse through the words using for loop.\r\n for i in words:\r\n # Traverse through all the characters of the word using another for loop.\r\n for letter in i:\r\n # Check to see if the letter is a digit, and if so, print it.\r\n if(letter.isdigit()):\r\n print(letter)\r\n<\/pre>\n
Enter the name of the given file : samplefile.txt \r\n 1\r\n 1\r\n 2\r\n 3\r\n 9\r\n 4\r\n 5\r\n 6\r\n 7\r\n 9\r\n 8\r\n 3\r\n 3<\/pre>\n
\n
hel11l2o th3is i9s B4TechGe5eks Onl6ine Cod7ing Platf9orm fo8r Bt3ech Stude3nts<\/pre>\n
\n