[英]Multiple files as function input
I have several csv files of a name sequence where the numbers change我有几个 csv 名称序列的文件,其中数字发生变化
I need to call a function that takes all these file at once我需要调用 function 一次获取所有这些文件
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 function定义如下
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当我尝试使用“名称”作为输入调用 function 时,如下所示
x, y =getBpms(names, 0)
I seems to inter in an infinite loop and no output was shown.我似乎陷入了无限循环,没有显示 output。
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您需要迭代
for filename in names:
使用“pd.read_csv()”,如果您尝试将多个 csv 连接成一个 DataFrame,那么您可以查看以下问题: 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.您应该在 getBpms 中创建一个循环
getBpms
以单独读取文件 pandas 没有多个文件读取属性。
...
for file_name in filename_list:
twiss = pd.read_csv(...)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.