2017-10-15 376 views
1

我正在使用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的

回答

0

还不确定原因,但问题是在这行代码:

Root_path = c_input_df.loc["HF Modeling folder full path"] 

如果我使用的方法astype(STR).Parameters

root_path = c_input_df.loc["HF Modeling folder abs path"].astype(str).Parameters 

我得到了我正在寻找的结果,只是字符串,让我们看看它:

前:

root_path = c_input_df.loc["HF Modeling folder abs path"] #.astype(str).Parameters 
    print root_path 

Parameters L:/Data/Jose/2.-SuperFO_testing/1612-02_University_9-319H_FleurDeLis/Internal Data/HF Modeling/ 
Name: HF Modeling folder abs path, dtype: object 

...当我添加参数末

root_path = c_input_df.loc["HF Modeling folder abs path"] .astype(str).Parameters 
    print root_path 

L:/Data/Jose/2.-SuperFO_testing/1612-02_University_9-319H_FleurDeLis/Internal Data/HF Modeling/ 

我有我的问题解决了,但我想更好地了解为什么导入时,这种行为数据来自数据框。

RGDS,

Pegaso的