0% found this document useful (0 votes)
14 views4 pages

CSV New

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Working with csv files in Python

What Is a CSV File?


A CSV file (Comma Separated Values file) is a type of plain text file that uses specific
structuring to arrange tabular data. Because it’s a plain text file, it can contain only
actual text data—in other words, printable ASCII or Unicode characters.

The structure of a CSV file is given away by its name. Normally, CSV files use a
comma to separate each specific data value. Here’s what that structure looks like:

column 1 name,column 2 name, column 3 name


first row data 1,first row data 2,first row data 3
second row data 1,second row data 2,second row data 3
...
Notice how each piece of data is separated by a comma. Normally, the first line
identifies each piece of data—in other words, the name of a data column. Every
subsequent line after that is actual data and is limited only by file size constraints.

In general, the separator character is called a delimiter, and the comma is not the only
one used. Other popular delimiters include the tab (\t), colon (:) and semi-colon (;)
characters. Properly parsing a CSV file requires us to know which delimiter is being
used.

Where Do CSV Files Come From?


CSV files are normally created by programs that handle large amounts of data. They
are a convenient way to export data from spreadsheets and databases as well as
import or use it in other programs. For example, you might export the results of a data
mining program to a CSV file and then import that into a spreadsheet to analyze the
data, generate graphs for a presentation, or prepare a report for publication.

CSV files are very easy to work with programmatically. Any language that supports
text file input and string manipulation (like Python) can work with CSV files directly.

Parsing CSV Files With Python’s Built-in CSV Library


The csv library provides functionality to both read from and write to CSV files.
Designed to work out of the box with Excel-generated CSV files, it is easily adapted to
work with a variety of CSV formats. The csv library contains objects and other code to
read, write, and process data from and to CSV files.
Advantages of CSV :-

1. CSV files can be opened or edited by text editors like notepad.


2. In data-warehouse, CSV follows a fairly flat, simple schema.
3. Any programming language to parse CSV data is trivial, generating it is
extremely easy.
4. CSV is safe and can clearly differentiate between the numeric values and text.
CSV does not manipulate data and stores it as-is.
5. In CSV, you write column headers only once where as in Excel, you have to
have a start tag and end tag for each column in each row.
6. Importing CSV files can be much faster, and it also consumes less memory.

The csv module’s reader and writer objects read and write sequences.
Programmers can also read and write data in dictionary form using
the DictReader and DictWriter classes. Each row read from the csv file is
returned as a list of strings.

reader() Return a reader object which will iterate over lines in the
given csvfile. csvfile can be any object which supports the iterator protocol and
returns a string— file objects and list objects are both suitable. Each row read
from the csv file is returned as a list of strings. No automatic data type
conversion is performed.

Writing to a CSV file


Writing Nested Lists to CSV
# importing the csv module
import csv

# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']

# data rows of csv file


rows = [['Alok','CS','2','9.0'],['Kamal','COE','2','9.1'],
['Lalit','IT','2','9.3'],['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],['Pankaj', 'EP', '2',
'9.1']]

# name of csv file


filename = "university_records.csv"

# writing to csv file


with open(filename, 'w',newline=””) as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Let us try to understand the above code in pieces.
• fields and rows have been already defined. fields is a list containing all the field
names. rows is a list of lists. Each row is a list containing the field values of that
row.
• with open(filename, 'w',newline=””) as csvfile:
• csvwriter = csv.writer(csvfile)
Here, we first open the CSV file in WRITE mode. The file object is named
as csvfile. The file object is converted to csv.writer object. Newline=”” avoids
giving extra blenks lines between records We save the csv.writer object
as csvwriter.
• csvwriter.writerow(fields)
Now we use writerow method to write the first row which is nothing but the field
names.
• csvwriter.writerows(rows)
We use writerows method to write multiple rows at once.

#writing data to CSV file 1 by 1


def WriteData( ):
r=['name','roll','per']
ans='yes'
with open ("data1.csv","a",newline="") as f:
dwriter=csv.writer(f,delimiter=",")# Creating Object of writer class
dwriter.writerow(r)#Writing header Row
while ans=='yes': #Getting Data input
na=input("Enter name ")
rr=input("Enter roll ")
pp=input("Enter Percentage ")
dwriter.writerow( [na,rr,pp] )#Writing record to file
ans=input("More ")
#Searching Record With Percentage More than 90, where data is stored is stoterd in

data1.csv as [roll, name, percentage]

def SearchRecord( ):
with open ("data1.csv","r",newline="") as f:
dread=csv.reader(f,delimiter=",")
c=0
for r in dread:
if c==0: #To print Header row
print(r)
else:
if int(r[2])>=90:#String need to be Converted
print(r)
c=c+1
print(dread.line_num)#returns number of lines read

You might also like