【发布时间】:2018-03-27 09:02:47
【问题描述】:
我正在使用 pandas 编写一个 Python 代码,该代码将打开一个 .csv 文件并读取一些参数,这些参数稍后将用作另一个模块的输入。除了代码必须读取的参数之外,我的内部网络中还有其他 .csv 文件的位置(路径),这些文件包含稍后必须合并到最终输出中的数据。我的问题是打开这些文件;除非我明确定义路径(而不是使用允许我遍历最终代码所需的所有 .csv 文件的引用变量),否则我会得到 ValuError: Invalid file path or buffer object type: .
我尝试在路径中添加单引号和双引号,但这没有帮助。有人可以帮我弄清楚我做错了什么吗?
以下是我的代码片段,希望能帮助澄清问题。
提前感谢您的帮助!
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,
飞马
【问题讨论】: