Python Forum

Full Version: select Data input from csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to get data from a csv (text) file.
I only need to read one piece of data from the csv file.

I am using Python in Blender 2.79 to control the movement of a model using data from the csv file.

CSV FILE:
Line5=58,5,0.5,77.5,1,,,,,,10
Line6=51,5,12,85,1,0,0,,0,1.5,10
Line7=89,0,0,,,,,,,,10
Line8=13,5,29.0,,,,,,,,10

Example - I need to read the second number on the top line (5).
Then convert it to a number so I can use it to move my model by that amount.

See below, this is what I have tried so far but it reads the second number from all the lines and I can not use it to create a single variable.
import csv
data = open(path+"TL3.txt", "r")
reader = csv.reader(data, delimiter=',')
next(reader)
row = data.readlines()
for row in reader:
print(row[1])
data.close()
Quote:

Any help is greatly received. (I would not like to say how long I have been searching for an answer to this)

Sean
Hi, please put your code in Python code tags. If you want you can also put CSV file contents in output or inline tags. You can find help here.
I am reposting as I could not edit my original post (10 mins was up)
I need to get data from a csv (text) file.
I only need to read one piece of data from the csv file.

I am using Python in Blender 2.79 to control the movement of a model using data from the csv file.

CSV FILE:
Quote:Line5=58,5,0.5,77.5,1,,,,,,10
Line6=51,5,12,85,1,0,0,,0,1.5,10
Line7=89,0,0,,,,,,,,10
Line8=13,5,29.0,,,,,,,,10

Example - I need to read the second number on the top line (5).
Then convert it to a number so I can use it to move my model by that amount.

See below, this is what I have tried so far but it reads the second number from all the lines and I can not use it to create a single variable.

import csv
data = open(path+"TL3.txt", "r")
reader = csv.reader(data, delimiter=',')
next(reader)
row = data.readlines()
for row in reader:
print(row[1])
data.close()
Any help is greatly received. (I would not like to say how long I have been searching for an answer to this)

Sean
New question.
I now have this posted 2 times, How do I delete the original (which did not have Python code tags)
Threads are merged now, no worries about deleting.

And by the way, your code is not formatted properly (no indentation), so you may want to edit this.
Your attempt could be made to work with some effort. But it is a bit of a blend (no pun intended) of techniques, and thus not the most optimal/maintainable approach.

Here is a (generalized) piece of code that can serve as a good starting point:

import csv

line = 0
column = 1

with open(path+"TL3.txt", "r") as file:
    mycsv = csv.reader(file)
    mycsv = list(mycsv)
    text = mycsv[line][column]
    print(text)
with means using a context manager. Which is more safe when working with files, since it will close it for you when the program is done using it.
Perfect - it works

Thank you so much

Sean