
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read Text File into List or Array with Python
Python has built in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s). There are 6 modes of accessing files.
To read a text file we use read only ('r') to open a text file for reading. The handle is at the beginning of the document.
There are several ways to read a text file into a list or array using python
Using open() method
The open() function creates a file object from an open file. The filename and mode parameters are passed to the open() function.
Example
The following is an example in which a file is opened in read only mode using the open() function. The file is now read with the help of read() function. Then, the data of the file is printed using the print() function.
#Python program to read a text file into a list #opening a file in read mode using open() file = open('example.txt', 'r') #read text file into list data = file.read() #printing the data of the file print(data)
Output
On executing the above program, the following output is generated.
Coding encourages you to use logic and algorithms to create a program. When facing a new challenge, you need to follow a logical approach to solve the issue. Therefore, this is an exercise for your brain to train up your logical ability.
Logical thinking is not only about solving algorithms but also beneficial to your personal and professional life.
Using load() method from numpy
In Python, numpy.load() is used to load data from a text file, with the goal of being a quick reader for simple text files. The filename and mode parameters are passed to the open() function.
Example 1
In the following example, loadtxt is imported from the numpy module and the text file is read into the numpy array. The data is printed into the list using the print() function.
from numpy import loadtxt #read text file into NumPy array data = loadtxt('example.txt') #printing the data into the list print(data) print(data.dtype)
Output
On executing the above program, the following output is generated.
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.] Float64
The loadtxt() allows us to choose the data type while importing the text file, which is a good feature of loadtxt(). Let us use an integer to specify the text file to be imported into a NumPy array
Example 2
In the following example, loadtxt is imported from the numpy module. The text file is read into the numpy array with the help of the loadtxt() function. And then, the data is printed into the list using the print() function.
from numpy import loadtxt #read text file into NumPy array data = loadtxt('example.txt', dtype='int') #printing the data into the list print(data) print(data.dtype)
Output
On executing the above program, the following output is generated.
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14] int32
Using data.replace() method
Dataframe created with Pandas. In a dataframe, the replace() function is used to replace a string, regex, list, dictionary, series, number, and so on. Because of its various variations, this is an extremely rich function.
Example
In the following example, the file is opened in read mode and it is read using the read() function. The end of the line '\n' is replaced with ' ' and the text is splitted if further ?.' Is seen. The data is now printed as output using the print() function.
#program to read a text file into a list #opening the file in read mode file = open("example.txt", "r") data = file.read() # replacing end of line('/n') with ' ' and # splitting the text it further when '.' is seen. list = data.replace('\n', '').split(".") # printing the data print(list) file.close()
Output
On executing the above program, the following output is generated.
[' Coding encourages you to use logic and algorithms to create a program', 'When facing a new challenge, you need to follow a logical approach to solve the issue', 'Therefore, this is an exercise for your brain to train up your logical ability', ' Logical thinking is not only about solving algorithms but also beneficial to your personal and professional life', '']