Q. Write a Python program to write a nested Python list to a csv file in one go. After writing the CSV read the CSV file and display the content.

Answer :-

import csv

file = open( "Portal.csv","w",newline="" )
lst = eval(input("Enter a nested list :- "))
path_write = csv.writer( file)
path_write.writerows( lst )
file.close()
print()
print("Content of Portal.csv :-")
print()
file = open( "Portal.csv","r" )
data = csv.reader(file ,)
for i in data :
      print(i)

file.close()

Output : -

Enter a nested list :- [ [1,3], [2,4,6],[5,7,9,11],[8,0] ]

Content of Portal.csv :-

['1', '3']
['2', '4', '6']
['5', '7', '9', '11']
['8', '0']

>>> 


Enter a nested list :- [ [78,547,638,21],[785,412,3985,6412,745],[3256,789,456,21]]

Content of Portal.csv :-

['78', '547', '638', '21']
['785', '412', '3985', '6412', '745']
['3256', '789', '456', '21']

>>> 


3 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post