Home »
Python »
Python programs
Read contents of a file using readline() method and manipulating it in Python
Here, we are going to learn how to read contents of a file using readline() method and manipulating it in Python?
Submitted by Shivang Yadav, on February 11, 2021
Program Statement: Python program to read contents of a file and manipulate data while printing.
Program Description: We will read the contents of the file using readline() method and then append the data to the end after manipulating it.
We will use the concepts of file handling in python to append the contents to the file using readline() and append() method.
- The readline() method is used to read content from data line by line.
Steps to manipulate readed contents of file:
- Step 1: Open the file in append mode using 'r'.
- Step 2: Get the input data from the user and store it.
- Step 3: Manipulate the data using some operation.
- Step 4: Print the resultant data.
Program to illustrate the solution of the problem
F=open("data.dat","r")
while(True):
data=F.readline()
if(data==""):break
DL=data.split(",")
DL[2]=DL[2].rstrip("\n")
DL.append(int(DL[2])*20/100)
print(DL)
F.close()
Output:
['10032', 'John Doe', '45000', 9000.0]
['10323', 'Ram', '50000', 10000.0]
In the above code, we have read data from a file named 'data.dat'. And then perform the manipulation operation on data and print it.
Python file handling programs »