21

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?

smci
  • 32,567
  • 20
  • 113
  • 146
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160

3 Answers3

19

_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 :)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • More importantly, according to the [Jython docs](http://www.jython.org/javadoc/org/python/modules/_csv/_csv.html), it should never be referenced directly. This is why csv.py imports its functionality. – KBKarma Oct 18 '12 at 17:09
  • Thank you. That also explains why I can't find any reference to a Writer class - the "class" is implemented in C. – Larry Lustig Oct 18 '12 at 17:11
  • Hmm. I assume that Modules is part of the Python source distribution and that the resulting object code is linked to the Python executable? I have neither a Module sub directory nor very much C code in my Python27 directory (just a few files in site-packages). – Larry Lustig Oct 18 '12 at 17:14
  • @LarryLustig: yes, that's in the source code. See updated answer. – Fred Foo Oct 18 '12 at 17:18
  • 1
    Running on Windows, I don't see that directory (lib-dynload) or any file with _csv in the name. Doesn't matter, since I'm looking for Python examples of the Writer pattern. Thanks for the help! – Larry Lustig Oct 18 '12 at 17:33
  • @LarryLustig: that's the location on my Linux box. If you enter the same statements, you can see where the library is on yours. – Fred Foo Oct 18 '12 at 17:37
  • Thanks, I get ``, which is along the lines of what I was expecting (seeing as I couldn't find the file myself). – Larry Lustig Oct 18 '12 at 17:38
  • @LarryLustig: ah, excuse me. I thought all Python versions would print the path. But "built-in", in CPython, always means something's implemented in C (or C++, or Fortran). – Fred Foo Oct 18 '12 at 19:30
  • @FredFoo: on Windows binary distributions (e.g. Anaconda), **there is no ./Modules**, so the definitions of the symbols in _csv are very much hidden. If you find | grep the entire tree, you won't see any definitions. – smci Aug 27 '16 at 00:44
14

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)

Vladimir Sinenko
  • 4,629
  • 1
  • 27
  • 39
  • 1
    +1, I didn't know that was in PEP8 (although I was using the convention for my own C extensions). – Fred Foo Oct 18 '12 at 17:19
-1

An answer has already been accepted, but I wanted to add a few details as I had the same question as the OP.

A .csv file's content is just a comma separated document with values:

first,last
john,doe
...

You can test this by opening up a .csv file in notepad

If you want to create a .csv file you can write to it and you will get the csv file that you want.

with open('test.csv', 'w') as file:
    file.write('first,last\n')
    file.write('John,Doe\n')

Simple solution if you want to write to a .csv file without using "import csv".

Discoveringmypath
  • 1,049
  • 9
  • 23
  • This is not related to the question, which is about a particular built-in module in Python and not in any way about the contents of a file with a `csv1` extension. It's also, at the very least, quite incomplete as it doesn't discuss the issue of embedded commas and quoting which is central to csv processing. – Larry Lustig Jan 18 '18 at 19:09
  • @LarryLustig You are right, you ask a question and my answer doesn't answer the question that was asked. But reading the question, the overall goal was to write your own "writer". I stumbled upon this thread because I was searching for the same thing, then realized how easy it is to implement. So I posted to give that example to anyone else who finds this thread. Also I give a very basic example, and there is no issue with embedding commas the way I did; that is beyond the point. – Discoveringmypath Jan 18 '18 at 20:01