0

I have several csv files of a name sequence where the numbers change

I need to call a function that takes all these file at once

I tried to do it as following:

names = ["orm_x_CXY_" + str(i) + ".csv" for i in range(0,40)]

The function is defined as following

import pandas as pd
import numpy as np
def getBpms(filename, ipage):

    twiss = pd.read_csv(filename, skiprows=1,
                        names=['s_pos', 'beta_x', 'beta_y', 'dx', 'dy', 'closed_orbitx', 'closed_orbity', 'element_type', 'elements_strength'],
                        header=None, index_col=0)

    s = twiss.s_pos
    x = twiss.closed_orbitx
    y = twiss.closed_orbity
    etype = twiss.element_type
    xs = []
    ys = []
    i=0
    for i in range(len(etype)):
        if(etype[i] == 'BPM'):
           xs.append('BPM'+str(i))
           ys.append('BPM'+str(i))


    return np.array(xs), np.array(ys)

when i tried to call the function with "names" as input as following

x, y =getBpms(names, 0)

I seems to inter in an infinite loop and no output was shown.

ely66
  • 175
  • 1
  • 11
  • 1
    I guess `getBpms` only expects a single string filename as an input. I would suggest to generate a `for` loop, where you call `getBpms` with the individual elements of `names`. What do you think about that? – André Mar 10 '22 at 14:47

2 Answers2

0

You need to iterate each file for filename in names: with 'pd.read_csv()` , if you try to concatenate multiple csv's into one DataFrame then you could check this question: Import multiple csv files into pandas and concatenate into one DataFrame

import pandas as pd
import glob

path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob(path + "/*.csv")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

I hope its what you meant to do.

DanielGzgzz
  • 118
  • 1
  • 6
0

You should create a loop in the getBpms function to read files separately pandas don't have multiple file reading attribute.

    ...
    for file_name in filename_list:
       twiss = pd.read_csv(...)
    ...
tasozgurcem11
  • 81
  • 1
  • 2
  • I tried your suggestion, it works, the function returns now x and y, is it for the last called file only ? – ely66 Mar 10 '22 at 15:00
  • yes exactly so you should also put your other code block inside that loop maybe you can append them into a list. – tasozgurcem11 Mar 10 '22 at 15:26