Here is a sequence of examples that show how to read csv (into blender)
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
Most notably is the skipping of the first line using
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.
the old code is here
naive
this first gist will also print the newline, at the end of each csv line, this can be annoying.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
file = open('example.csv') | |
for line in file: | |
print(line) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
file = open('example.csv') | |
for line in file: | |
print(line, end='') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
file = open('example.csv') | |
for idx, line in enumerate(file): | |
print(idx,'-->', line, end='') | |
if idx == 10: | |
break |
splits the incoming line easily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Python also has a csv module, and we like a bit of convenience if we can handle it. Here is the documentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
import csv | |
file = csv.reader(open('example.csv', newline=''), delimiter=',') | |
for row in file: | |
print(row) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Here's another use of csv, and writing json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
__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