CSV is all called "Comma separated Values", which is a formatted file that consists of rows and columns, and separators can vary as needed.
The following is a CSV file:
Title,release Date,directorand now for Something Completely Different,1971,ian macnaughtonmonty Python and the Holy Grail, 1975,terry Gilliam and Terry jonesmonty python ' s life of Brian,1979,terry Jonesmonty Python Live at the Hollywood bowl,198 2,terry Hughesmonty Python ' s The meaning of Life,1983,terry Jones
CSV makes it easier to migrate data between different applications. You can export the data in bulk to a CSV format and then pour it into another application. Many applications need to export reports, often exported in CSV format, and then use the Excel tool for subsequent edits.
Print the release date and title, line by row:
For On open ("Samples/sample.csv"): title, year, Director = Line.split (",") print year, title
Use the CSV module to process:
Import Csvreader = Csv.reader (Open ("Samples/sample.csv")) for title, year, director in reader: print year, title
changing separators
Create a Csv.excel subclass and modify the delimiter to ";"
# File:csv-example-2.pyimport Csvclass SKV (csv.excel): # like Excel, but uses semicolons delimiter = ";" Csv.regi Ster_dialect ("SKV", SKV) reader = Csv.reader (open ("Samples/sample.skv"), "SKV") for title, year, director in reader: Print year, title
If you only change one or two parameters, you can set it directly in the reader parameter, as follows:
# file:csv-example-3.py Import CSV reader = Csv.reader (open ("Samples/sample.skv"), delimiter= ";") For title, year, director in reader: print year, title
Save data to CSV format
Generate a CSV file with Csv.writer.
# file:csv-example-4.py Import csvimport sys data = [ ("And now for Something Completely Different", 1971, "Ian Macna Ughton "), (" Monty python and the Holy Grail ", 1975," Terry Gilliam, Terry Jones "), (" Monty python ' s Life of Brian " , 1979, "Terry Jones"), ("Monty python Live at the Hollywood Bowl", 1982, "Terry Hughes"), ("Monty python ' s The Me Aning of Life ", 1983," Terry Jones ")] writer = Csv.writer (sys.stdout) for item in data: Writer.writerow (item)
Instance
Let's look at a more complete example of the code description in the comment:
Import csv# dialect is one of the parameters that you need to specify when accessing a CSV file to determine the data format of the CSV file # The following function enumerates which dialect the system supports, the default value is ' Excel ', and the user can # to derive a class from dialect. Use an instance of this class as the dialect parameter. Print csv.list_dialects () def test_writer (): # CSV file must be in binary mode open with open (' Eggs.csv ', ' WB ') as Csvfile:spamwriter = Csv.writer (CSVFile) spamwriter.writerow ([' Spam '] * 5 + [' Baked Beans ']) spamwriter.writerow ([' Spam ', ' lovely Spam ', ' Wonderful Spam ') def test_reader (): With open (' eggs.csv ', ' RB ') as Csvfile:spamreader = Csv.reader (csvfile) for R ow in Spamreader:print row# sniffer is used to infer the format of the CSV file, not very accurate def test_sniffer (): With open (' Eggs.csv ', ' WB ') as CSVFile: Spamwriter = Csv.writer (CSVFile, delimiter= ") spamwriter.writerow ([' Spam '] * 2 + [' Baked Beans ']) spamwriter.wri Terow ([' Spam ', ' lovely Spam ', ' Wonderful Spam ']) # Usually you need to specify the same file format as the writer to read the data correctly with open (' eggs.csv ', ' RB ') as CSVFile: Spamreader = Csv.reader (CSVFile, delimiter= ") for row in Spamreader:print ', '. Join (ROW) # If you don't know the file format, sniffer It could be useful. With OPen (' eggs.csv ', ' RB ') as CSVFile: # infers the file format with sniffer, resulting in dialect dialect = csv. Sniffer (). Sniff (csvfile.read) print dialect.delimiter, Dialect.quotechar # file re-moved to head csvfile.seek (0) # with Inferred dialect Create reader reader = Csv.reader (csvfile, dialect) for row in Reader:print ', '. Join (ROW)