2017-10-15 99 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的