Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to loop through a folder of 70 csv files. Each of the file has 2 columns which I want to store into two arrays (xData[] and yData[]). I have attached my sample code and I am unable to print the file name and my updated arrays. Kindly help me with this code

What I have tried:

<pre>path=r"E:\Users\ConfocalUser\Documents\GitHub\qudi"
files=glob.glob(os.path.join(path,'*.csv')
xData=[]
yData=[]
for file in files:
    print(file)
    with open(path,"r") as f_in:
        reader=csv.reader(f_in)
        next(reader)
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData.append(float_1)
                yData.append(float_2)
            except ValueError:
                continue
Posted
Updated 8-Jul-21 23:26pm
Comments
OriginalGriff 9-Jul-21 4:47am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

You could start by fixing the basic syntax and other errors:
Python
import os
import glob
import csv

path=r"E:\Users\ConfocalUser\Documents\GitHub\qudi"
files=glob.glob(os.path.join(path,'*.csv')) # add the second closing parenthesis here
xData=[]
yData=[]
for file in files:
    print(file)
    with open(file,"r") as f_in: # open 'file' not 'path'
        reader=csv.reader(f_in)
        next(reader)  # NB only use this if CSV has a header line
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData.append(float_1)
                yData.append(float_2)
            except ValueError:
                continue
print('xData:', xData) # show the results
print('yData:', yData)

Always test your code first with a few simple fixed test files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900