CSV stands for "comma separated values" is the most common import and export format for spreadsheets and databases. Python provides the csv module in which reader objects allow you to read the csv file and writer object to write a csv file. Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes.
Note : Python2.7 csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe.
Python Script to write testFile.csv :
Writing csv file is easy first you need to import csv module in your program and then csv.writer() will create a writer object and then csv.writer() object will create the empty csv file as per given arguments and later you can use writerow() to write the comma separated values.Look at the following program.
Run the above program and confirm the myFile.csv has been generated with its contents.
Quoting:
There are four different quoting options, defined as constants in the csv module.
QUOTE_ALL
Quote everything, regardless of type.
QUOTE_MINIMAL
Quote fields with special characters (anything that would confuse a parser configured with the same dialect and options). This is the default
QUOTE_NONNUMERIC
Quote all fields that are not integers or floats. When used with the reader, input fields that are not quoted are converted to floats.
QUOTE_NONE
Do not quote anything on output. When used with the reader, quote characters are included in the field values (normally, they are treated as delimiters and stripped).
Python Script to read testFile.csv :
For reading the csv file, first create the file object of file for which you want to read in read mode. Use reader() to create an object for reading data from a CSV file. The reader can be used as an iterator to process the rows of the file in order. Look at below program:
The output you will get something like this.
If you want to read a particular column then provide the index while iterating the csvReader list. Look at the below example in which i am reading just second column of CSV file.
The output will be something like a below:
Note : Open method have 2 argument open(filename,mode) , We have different following kind of modes:
r : When the file will only be read
w : When the file only writing (an existing file with the same name will be erased)
a : Opens the file for appending; any data written to the file is automatically added to the end
r+ : Opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files
***************************************
Note : Python2.7 csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe.
Python Script to write testFile.csv :
Writing csv file is easy first you need to import csv module in your program and then csv.writer() will create a writer object and then csv.writer() object will create the empty csv file as per given arguments and later you can use writerow() to write the comma separated values.Look at the following program.
import csv # Creating a file object in appending mode f=open('myFile.csv','ab')try: # Creating the writer object and myFile.csv csvWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) # Writing the csv header csvWriter.writerow(['SNO','Employee','Salary']) # Writing the content into csv file csvWriter.writerow([1,"Jon Baker","20,000"]) csvWriter.writerow([2,"Abhishek","60,000"]) csvWriter.writerow([3,"Dinesh Kumar","28,000"]) csvWriter.writerow([4,"Anita","22,000"]) csvWriter.writerow([5,"Ajay Singh","26,000"]) finally: f.close()
Run the above program and confirm the myFile.csv has been generated with its contents.
Quoting:
There are four different quoting options, defined as constants in the csv module.
QUOTE_ALL
Quote everything, regardless of type.
QUOTE_MINIMAL
Quote fields with special characters (anything that would confuse a parser configured with the same dialect and options). This is the default
QUOTE_NONNUMERIC
Quote all fields that are not integers or floats. When used with the reader, input fields that are not quoted are converted to floats.
QUOTE_NONE
Do not quote anything on output. When used with the reader, quote characters are included in the field values (normally, they are treated as delimiters and stripped).
Python Script to read testFile.csv :
For reading the csv file, first create the file object of file for which you want to read in read mode. Use reader() to create an object for reading data from a CSV file. The reader can be used as an iterator to process the rows of the file in order. Look at below program:
import csv # Creating a file object in appending mode f=open('myFile.csv','rb') try: csvReader=csv.reader(f) for row in csvReader: print row finally: f.close()
The output you will get something like this.
['SNO', 'Employee', 'Salary'] ['1', 'Jon Baker', '20,000'] ['2', 'Abhishek', '60,000'] ['3', 'Dinesh Kumar', '28,000'] ['4', 'Anita', '22,000'] ['5', 'Ajay Singh', '26,000']
If you want to read a particular column then provide the index while iterating the csvReader list. Look at the below example in which i am reading just second column of CSV file.
import csv # Creating a file object in read mode f=open('myFile.csv','rb') try: csvReader=csv.reader(f) for row in csvReader: print row[1] finally: f.close()
The output will be something like a below:
Employee Jon Baker Abhishek Dinesh Kumar Anita Ajay Singh
Note : Open method have 2 argument open(filename,mode) , We have different following kind of modes:
r : When the file will only be read
w : When the file only writing (an existing file with the same name will be erased)
a : Opens the file for appending; any data written to the file is automatically added to the end
r+ : Opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files
***************************************
No comments:
Post a Comment