Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 14, 2011

Blender 2.5 Python Reading CSV

Here is a sequence of examples that show how to read csv (into blender)

naive

this first gist will also print the newline, at the end of each csv line, this can be annoying. While this version drops the newline. If you are seeing the data in the csv for the first time then it is useful to set a break at 10 lines to get a comprehension of the data. here enumerate is used to track the element index without setting up a counter first.

splits the incoming line easily


Python also has a csv module, and we like a bit of convenience if we can handle it. Here is the documentation. That might be more useful to you. You can still use the enumerate method on file

Here's another use of csv, and writing json

Most notably is the skipping of the first line using __next__().

old text

The reading and parsing has nothing to do with blender specifically, I include it because i'm going to plot it using blender. The file i was reading has over 500,000 lines, and element 2,3 and 4 of each line describes a coordinate.

Doing stuff with the read dataset

While the method of reading data is pretty general, the implementation of the code after (or during the read of) the data can be very specialized. Here is a rather lengthy example of one of the first longer bits of code I ever wrote. The splitting of data on commas happens in the initFunction.

the old code is here