0% found this document useful (0 votes)
26 views

FH6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
26 views

FH6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
CSV FILE en a CSV is a simple file format used to store tabular data, such as a spreadsheet or database. co Files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOffice Calc. a | CSV stands for "comma-separated values“ « A comma-separated values file is a delimited text file that uses a comma to separate values. a Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format CSV tile handling in Python NS 0 To perform read and write operation with CSV file, we must import €SV module. 0 open() function is used to open file, and return file object. Reading from CSV file LLL 0 import csv module a Use open() to open csv file, it will return file object. Oo Pass this file object to reader object. co Perform operation you want Example : Reading from CSV File = LL +-Arnit, 6000 2,Suresh Kumar,8000 3,Gabbar,75000 import csv ‘myfileccsy ith open('myfile.csv') as csvfile: myreader = csv. reader (csvfile, delimiter=",') print ("$10s"$"EMPNO", "$20s"$"EMP NAME", "$10s"$"SALARY") for row in myreade: print ("$10s"$row[0],"%20s"$row[1],"$10s"$row[2]) EMPNO EMP NAME SALARY 1 Amit 6000 2 Suresh Kumar 8000 3 Gabbar 75000 oho See ACM UP Rice at land Example : Reading from CSV File a 1,Amit,6000 2,Suresh Kumar,8000 3,Gabbar,75000 import csv with open('myfile.csv') as csvfile: myreader = csv.reader (csvfile, delimit: ) print ("$10s"$"EMPNO","$20s"%"EMP NAME", "$108"$"SAZARY") print (" for row in myreader: print ("$10s"%row[0],"%20s"$row[1] ,"$10s"trow[2]) First line :- is to import csv module, this is necessary to perform operations on csv file Second line :- is to open file to read using “with” block, this block will ensure opened file will be closed automatically after this block even in case of runtime error also. Example : Reading from CSV File a 1,Amit,6000 2,Suresh Kumar,8000 3,Gabbar,75000 import csv with open('myfile.csv') as csvfile: myreader = csv. reader (csvfile,delimiter=",') print ("$10s"%"EMPNO", "$20s"%"EMP NAME", "$10s"%"SALARY") print ("== for row in myreader: print ("t10s"%row[0],"%20s"%row[1],"%10s"%row[2]) Third line: to create reader object, to perform read operation on csv file, the first parameter is the name of csv file, and second parameter is delimiter ie. on what basis value of different field to read, by default it is comma(,) so this is optional if not given it will be by default comma, however if any other character is used in file then we must specify this. myreader will become csv reader object to read csv file. Example : Reading from CSV File nnn 1,Amit,6000 2,Suresh Kumar,8000 3,Gabbar,75000 import csv with open('myfile.csv') as cevfile: myreader = csv. reader (csvfile, delimite: print ("$10s"$"EMPNO", "$20s"8"EMP NAME", "$10s"#"SALARY") ty print (" for row in myreader: print ("$10s"$row[0],"820s"trow[1],"$10s"$row[2]) Fourth line : this is formatted string to specify width of each column, so that output appears properly aligned in their width, here %10s means 10 space for EMPNO, and so on. This is helpful in printing output in formatted way. Sixth line :- this line is used to read each row from file one by one and store in row ie. all the comma separated values will be stored in row in different index variable. Example : keaaing trom CoV File 2 1,Amit,6000 2,Suresh Kumar,8000 3,Gabbar,75000 import csv with open('myfile.csv') as csvfile: myreader = csv. reader (csvfile,delimiter=",') print (*WiOatS TERENOT RDAT SCENE MONET, “$LOntS TANABE") print ("sssssssssssssssssscsssssssssssssssssssso y for row in myreader: print ("$10s"$row[0],"%20s"$row[1],"$10s"$row[2]) Seventh Line :- this line is simply printing all the values in row variable by specifying index i.e. index 0 will be for (according to first row in file) 1, 1 for ‘Amit’, 2 for 6000. here also we used same width as for heading so that both heading and data are aligned properly Example : Reading from CSV File open('myfile.csv') as csvfile: myreader = csv. reader (csvfile, delimiter=',') print ("$10s"$"EMPNO","$20s"$"EMP NAME", "$i0s” EMPNO EMP NAME SALARY 1 Amit 6000 2 Suresh Kumar 8000 z Gabbar 75000 Here you can see the output is properly aligned in __ their assigned width CTR on ee AO mkt teas

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy