Here is the example that it takes input from the user and create CSV file in Python. CSV stands for Comma Separated Values. It is a type of plain text file which uses specific structuring to arrange tabular data. It contains actual text data.
What is CSV file
- These files are easy to work programmatically and can handle large amount of data. As a programming part, it is a common requirement to read and write data w.r.t. csv files by using python csv library.
- If there is a requirement of huge data or numerical analysis, we can look forward for pandas library which has csv parsing capabilities as well. But here we will only discuss about reading csv files using python library only.
How to write data to CSV file
Starting from import of csv, each step explained with its purpose.
import csv
with open("mycsvfile1.csv","w") as myfile1:
mywrite=csv.writer(myfile1) # CS1
print(type(mywrite)) # CS2
while True:
myroll_no=input("Enter Student Roll No:") # CS4
myname=input("Enter Student Name:") # CS5
myage=input("Enter Student Age:") # CS6
mycontactno=input("Enter Student contact number:") # CS7
print(myfile1.closed)
mywrite.writerow([myroll_no, myname, myage, mycontactno]) # CS8
check = input("Do you want to enter one more student data: (Yes|No): ") # CS9
if check.upper() == 'NO': # CS10
break
print("Total Students data written to csv file successfully")
Output
The result creates CSV file with the user input.
<class ’_csv.writer’>
Enter Student Roll No:1
Enter Student Name:Saurabh
Enter Student Age:10
Enter Student contact number:8907654321
Do you want to enter one more student data: (Yes|No): Yes
Enter Student Roll No:2
Enter Student Name:Divya
Enter Student Age:10
Enter Student contact number:9876543210
Do you want to enter one more student data: (Yes|No): No
Total Students data written to csv file successfully
Step-by-step explanation of the program
In CS1, a csv writer object is returned to write data and will convert the user data to a delimited string.
In CS2, the type is <class ’_csv.writer’>.
In CS3, the string is written into csv files using the writerow() function. The column headers are passed as a list which will be converted to a delimited string and written into csv file.
In CS4, user is prompted to enter the roll no.
In CS5, user is prompted to enter the name.
In CS6, user is prompted to enter the age.
In CS7, user is prompted to enter the contact number.
In CS8, user is passing roll no, name, age and contact number as a list which are converted to a delimited string and written into the csv file.
In CS9, the user is prompted to enter more student data if required.
In CS10, the condition is getting checked if the user entered ‘No’ which will be automatically converted into upper case. If user entered Yes, then more student data will be written to the file else the code will get exit as the control comes out of the while loop.
Reading CSV file
Here’s the way to read CSV file in python
import csv
myfile1=open("mycsvfile1.csv", 'r')
myfile1=csv.reader(myfile1) #returns csv reader object
print(type(myfile1))
mydata=list(myfile1)
print(mydata) # list of list returning
for myrow in mydata: # iterating each element as list
for mycol in myrow:# iterating each element of list
print(mycol,'\t', end='')
Related