TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python CSV: csv.reader and Sniffer

Use the csv module to read comma separated values files. Call csv.reader and use Sniffer.
CSV. Reading CSV files is a common task. It can be accomplished in many ways: the split() method is often used. But the csv module provides more built-in support. It can sniff the format of a file. We test this helpful type.
Example. We first open a file in a "with" statement. The syntax to do this is a little different but straightforward. Then we call csv.reader, which creates a reader. We specify here that the delimiter is a comma.

Tip: The reader can then be looped over. Each row is a list of string values. It can be used like any other list.

Delimiter: This must be a 1-character string. In my testing, a TypeError is encountered if a different length is specified.

Input file.csv one,two,three,4 cat,dog,mouse,5 Python program that uses CSV file import csv # Open CSV file. with open("C:/programs/file.csv", newline="") as f: # Specify delimiter for reader. r = csv.reader(f, delimiter=",") # Loop over rows and display them. for row in r: print(row) Output ['one', 'two', 'three', '4'] ['cat', 'dog', 'mouse', '5']
Sniffer. Sometimes a program may not know the format of the csv files. The delimiter characters may vary between files. With Sniffer, a class, we can handle this situation. We call the sniff method to generate a dialect.
The dialect returned by sniff() is then passed to the reader initialization method. A dialect class stores information about the delimiter characters and how the file is formatted. We then use the reader.

Tip: If you know the file format, you can pass a string literal to sniff() without reading a sample of the file.

Python program that uses Sniffer import csv # Open the file. with open("C:/programs/file.csv") as f: # Get dialect from Sniffer. # ... Pass a sample to sniff. dialect = csv.Sniffer().sniff(f.read(1024)) # Seek to beginning. f.seek(0) # Read file and print its rows. r = csv.reader(f, dialect) for row in r: print(row) Output ['one', 'two', 'three', '4'] ['cat', 'dog', 'mouse', '5']
Discussion. The csv module supports more than is shown on this page. It can also write CSV files. And it provides some features that are superior to using split() such as quote character support.CSV: Python.org
Summary. A powerful CSV file parser is not always needed in programs. Often the format is well-known. But when more features are required, csv.reader and its dialect class are helpful. They make programs easier to develop.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf