Python Forum
import columns of data from local csv file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: import columns of data from local csv file (/thread-27412.html)



import columns of data from local csv file - CatherineKan - Jun-05-2020

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]


RE: import columns of data from local csv file - hussainmujtaba - Jun-25-2020

You can use pandas data frame to import these csv or excel files


RE: import columns of data from local csv file - ricslato - May-10-2021

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'