Advertisements
Advertisements
प्रश्न
Write a Python program to modify an existing file.
Advertisements
उत्तर
Coding:
import csv ,
row = [‘3: ‘Meena’Bangalore’]
with opent’student.csv; ‘r’) as readFile:
reader = csv.reader(readFile)
lines = list(reader) # list()- to store each
row of data as a list
lines [3] = row
with open (student.csv, ‘w’) as writeFile:
# returns the writer object which converts the user data with delimiter
writer = csv.writer(writeFile)
#writerows()method writes multiple rows to a csv file
writer, writerows(lines)
readFile.close()
writeFile. close()
Original File:
| Roll No | Name |
City |
| 1 | Harshini, | Chennai |
| 2 | Adhith, | Mumbai |
| 3 | Dhuruv | Bangalore |
| 4 | egiste, | Tirchy |
| 5 | Venkat | Madurai |
Modified File after the coding:
| Roll No | Name |
City |
| 1 | Harshini, | Chennai |
| 2 | Adhith, | Mumbai |
| 3 | Meena | Bangalore |
| 4 | egiste, | Tirchy |
| 5 | Venkat | Madurai |
APPEARS IN
संबंधित प्रश्न
Which of the following is a string used to terminate lines produced by the writer()method of the csv module?
What is the output of the following program? import csv
d=csv.reader(open('c:\PYPRG\ch13\city.csv'))
next(d)
for row in d:
print(row)
if the file called “city.csv” contain the following details
| chennai,mylapore |
| mumbai,andheri |
Making some changes in the data of the existing file or adding more data is called ______
What will be written inside the file test.csv using the following program import csv
D = [['Exam'],['Quarterly'],['Halfyearly']]
csv.register_dialect('M',lineterminator = '\n')
with open('c:\pyprg\ch13\line2.csv', 'w') as f:
wr = csv.writer(f,dialect='M')
wr.writerows(D)
f.close()
How will you sort more than one column from a CSV file? Give an example statement.
