Most of the time, we process data from a file so that we can manipulate it from memory. This data can be numeric, string, or a combination of both. In this article, I will discuss how to open a file for reading with the built-in function open()
and the use of Pandas
library to manipulate data in the file. This also includes reading the contents of a file line by line and saving the same to a list.
Things To Learn in This Article
- Open the file for reading
- Read the contents of a file line by line
- Store the read lines into a list data type
- For loop
- List comprehension
- Readlines
- Readline
- Read file using pandas
1. Open the File for Reading
Python's built-in function open()
can be used to open a file for reading and writing. It is defined below based on the Python documentation.