Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "_csv" in Python?

In attempting to read the source code for the csv.py file (as a guide to implementing my own writer class in another context) I found that much of the functionality in that file is, in turn, imported from something called _csv:

 from _csv import Error, __version__, writer, reader, register_dialect, \
                  unregister_dialect, get_dialect, list_dialects, \
                  field_size_limit, \
                  QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE, \
                  __doc__

I cannot find any file with this name on my system (including searching for files with the Hidden attribute set), although I can do import _csv from the Python shell.

What is this module and is it possible to read it?

like image 629
Larry Lustig Avatar asked Oct 18 '12 17:10

Larry Lustig


People also ask

What is CSV used for in Python?

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

What is CSV file and its uses?

Data tables are presented in Comma Delimited, CSV text file format. Although this file format allows for the data table to be easily retrieved into a variety of applications, they are best viewed within one that will allow one to easily manipulate data that is in columnar format.


2 Answers

_csv is the C "backbone" of the csv module. Its source is in Modules/_csv.c. You can find the compiled version of this module from the Python command prompt with:

>>> import _csv
>>> _csv
<module '_csv' from '/usr/lib/python2.6/lib-dynload/_csv.so'>

There are no hidden files in the Python source code :)

like image 158
Fred Foo Avatar answered Sep 20 '22 12:09

Fred Foo


Not to disagree with larsmans answer.

There is an official explanation of the module naming convention in PEP8:

When an extension module written in C or C++ has an accompanying Python module that provides a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g. _socket)

like image 32
Vladimir Sinenko Avatar answered Sep 19 '22 12:09

Vladimir Sinenko