Python Forum

Full Version: import columns of data from local csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]
You can use pandas data frame to import these csv or excel files
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'