Python Forum
import columns of data from local csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import columns of data from local csv file
#1
Hello I am a newbie in python, but I need to write some python code in another software.
I need to import two csv files, each of them has 200 columns of data.

The attached screenshot shows what I have done so far, it successfully created 400 empty attributes, but while importing all columns of data, it seemed only worked for the first column of the first file as shown below. I do not know what is wrong with my nested for loops.

The spreadsheet in the back of my script interface is just the result of importing data into my Houdini software.

And I also attach my code as follows:

import hou
import csv
import os

node = hou.pwd()
geo = node.geometry()

for i in range(200):
    geo.addAttrib(hou.attribType.Point,"sol_conc%d"%(i),0.0)
    geo.addAttrib(hou.attribType.Point,"water_conc%d"%(i),0.0)
    
directory = node.evalParm('directory')
maxrows = node.evalParm('maxrows')

def main():
    if not directory: return
    if not os.path.isdir(directory): return
    
    files = []
    for name in os.listdir(directory):
        if name.endswith('.csv'):
            files.append(os.path.join(directory,name))
        files.sort()
    with open(files[0]) as fs:
        readers = csv.reader(fs,delimiter=',')
        for j in range(200):
            for row in readers:
                point = geo.createPoint()
                point.setAttribValue("sol_conc%d"%(j),float(row[j]))
                
    with open(files[1]) as fw:
        readerw = csv.reader(fw,delimiter=',')
        for k in range(200):
            for row in readerw:
                point = geo.createPoint()
                point.setAttribValue("water_conc%d"%(k),float(row[k]))

main()
[Image: Screenshot-61.png]
Reply
#2
You can use pandas data frame to import these csv or excel files
Reply
#3
use pandas read_csv()

import pandas as pd
df = pd.read_csv ('data.csv')
print(df)

Use your filename with full path instead of 'data.csv'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to import an xml file to Pandas sjhazard 0 448 Jun-08-2021, 08:19 PM
Last Post: sjhazard
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 724 Feb-20-2021, 08:25 PM
Last Post: perfringo
  how to filter data frame dynamically with the columns psahay 0 777 Aug-24-2020, 01:10 PM
Last Post: psahay
  Can the data types be different for different columns? Robotguy 2 919 Aug-19-2020, 09:24 PM
Last Post: Robotguy
  Unable to import data from Stata-press website Salahaddin 1 947 Jun-08-2020, 04:23 AM
Last Post: buran
  Import Excel File that Starts with Number kiki1113 1 1,609 Dec-20-2018, 07:13 PM
Last Post: Larz60+
  Trying to import JSON data into Python/Pandas DataFrame then edit then write CSV Rhubear 0 2,774 Jul-23-2018, 09:50 PM
Last Post: Rhubear
  manipulating .csv file into columns of selected data Karen_Masila 2 1,621 Feb-14-2018, 06:50 AM
Last Post: Karen_Masila
  Replacing values for specific columns in Panda data structure Padowan 1 13,335 Nov-27-2017, 08:21 PM
Last Post: Padowan
  import/use data from text files MichealPeterson 1 2,058 Jun-28-2017, 08:51 AM
Last Post: buran

Forum Jump:

User Panel Messages

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