简体   繁体   中英

Multiple files as function input

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.

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.

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(...)
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM