我正在使用pandas编写一个Python代码,它将打开一个.csv文件并读取稍后将用作另一个模块的输入的一些参数。沿着代码必须读取的参数,我的内部网络中存在其他.csv文件的位置(路径),其中包含必须稍后并入最终输出中的数据。我的问题是打开这些文件;除非我明确地定义了路径(而不是使用一个引用变量,它将允许我遍历最终代码需要的所有.csv文件),否则我会得到ValuError:无效的文件路径或缓冲区对象类型:。在Python中打开路径的问题
我尝试在路径中添加单引号和双引号,但这并没有帮助。有人能帮我弄清楚我做错了什么吗?
下面是我的代码片段,希望能够帮助澄清问题。
在此先感谢您的帮助!
Root_path = c_input_df.loc["HF Modeling folder full path"]
Input_path = Root_path + c_input_df.loc["FO_Input_Files folder name & location"]
下一页细胞
Input_path
Parameters C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/
dtype: object
下一页细胞
well_name
Parameters 'UNI-09'
Name: Well name, dtype: object
#those two strings (Input path and well_name) are used to tell the path and part of the name of the .csv file to open
下一页细胞
#This is the prefered method to read the dataframe:
FT_file = pd.read_csv(Input_path + "FT-" + well_name + ".csv")
#But it gives the following error:
ValueError: Invalid file path or buffer object type: <class 'pandas.core.series.Series'>
下一页细胞
#Since the method above gives an error, I used the explicit path:
FT_file = Input_path + "FT-" + well_name + ".csv"
FT_file
Parameters C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/FT-UNI-09.csv
dtype: object
#When I try the path directly in the pd.read_csv function, it reads the file
FT_file = pd.read_csv("C:/Users/Pegaso/AnacondaProjects/2.-SuperFO/2.Projects/Client_ABC/Internal Data/HF Modeling/FO_Input_Files/1.-Model_13102017/UNI-09_original/FT-UNI-09.csv")
FT_file
Par_1 Par_2 Par_3
0 Units_1 Units_2 Units_3
1 6630 2448.270301 3659.999055
2 6647.99982 2448.270301 3659.999055
我希望我能让自己明白,如果情况并非如此,请让我知道,我会尝试更详细地解释问题。
RGDS,
Pegaso的