【问题标题】:Problems opening a path in Python在 Python 中打开路径的问题
【发布时间】: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,

飞马

【问题讨论】:

    标签: python csv path


    【解决方案1】:

    还不知道为什么,但问题出在这行代码中:

    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,

    飞马

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-24
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多