Python Forum
select Data input from csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
select Data input from csv file
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
New question.
I now have this posted 2 times, How do I delete the original (which did not have Python code tags)
Reply
#5
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.
Reply
#6
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.
Reply
#7
Perfect - it works

Thank you so much

Sean
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 408 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 344 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Input network device connection info from data file edroche3rd 6 1,023 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,036 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  select files such as text file RolanRoll 2 1,172 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Showing an empty chart, then input data via function kgall89 0 978 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 2,127 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Reading an Input File DaveG 1 1,250 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  input data validation plumberpy 2 1,781 Aug-11-2021, 12:04 PM
Last Post: plumberpy
  Select data between DW felmback 2 2,013 Mar-21-2021, 01:49 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020