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.
import bpy
file = open('example.csv')
for line in file:
print(line)
view raw csv_python_1.py hosted with ❤ by GitHub
While this version drops the newline.
import bpy
file = open('example.csv')
for line in file:
print(line, end='')
view raw csv_python_2.py hosted with ❤ by GitHub
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.
import bpy
file = open('example.csv')
for idx, line in enumerate(file):
print(idx,'-->', line, end='')
if idx == 10:
break
view raw csv_python_3.py hosted with ❤ by GitHub
here enumerate is used to track the element index without setting up a counter first.

splits the incoming line easily
import bpy
file = open('example.csv')
# you will notice the trailing endline character \n,
# or some equivalent whitespace character.
for idx, line in enumerate(file):
separated = line.split(',')
print(idx,'-->', separated)
if idx == 10:
break
view raw csv_python_4.py hosted with ❤ by GitHub


Python also has a csv module, and we like a bit of convenience if we can handle it. Here is the documentation.
import bpy
import csv
file = csv.reader(open('example.csv', newline=''), delimiter=',')
for row in file:
print(row)
view raw csv_python_5.py hosted with ❤ by GitHub
That might be more useful to you. You can still use the enumerate method on file
import bpy
import csv
file = csv.reader(open('example.csv', newline=''), delimiter=',')
for idx, row in enumerate(file):
print(idx, row)
if idx == 10:
break
view raw csv_python_6.py hosted with ❤ by GitHub


Here's another use of csv, and writing json

import os
import csv
import json
filename = 'some.csv'
some_dict = {}
# for the smart, read http://docs.python.org/py3k/library/csv.html
def open_csv_test(filename):
#csvfile = open(filename, 'r', encoding='ISO-8859-15', newline='')
csvfile = open(filename, 'r', newline='')
ofile = csv.reader(csvfile, delimiter=',')
# skip the first line, usually reserved for describing the content
ofile.__next__() # or next(ofile)
for row in ofile:
try:
# do something with your some_dict here
print(row)
except:
print("failed at")
print(row)
with open('some_constructed.json', 'w') as wfile:
wfile.write(json.dumps(some_dict, sort_keys=True, indent=4))
open_csv_test(filename)
view raw csvandjson.py hosted with ❤ by GitHub
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