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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

9 Comments

More importantly, according to the Jython docs, it should never be referenced directly. This is why csv.py imports its functionality.
Thank you. That also explains why I can't find any reference to a Writer class - the "class" is implemented in C.
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).
@LarryLustig: yes, that's in the source code. See updated answer.
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!
|
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)

1 Comment

+1, I didn't know that was in PEP8 (although I was using the convention for my own C extensions).
-2

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".

2 Comments

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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.