在Python中打开路径时出现问题 [英] Problems opening a path in Python

查看:2453
本文介绍了在Python中打开路径时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pandas编写Python代码,这将打开一个.csv文件并读取一些参数,这些参数稍后将用作另一个模块的输入.沿着代码必须读取的参数,我的内部网络中还有其他.csv文件的位置(路径),这些文件包含必须稍后添加到最终输出中的数据.我的问题是打开这些文件.除非我明确定义路径(而不是使用引用变量使我可以遍历最终代码需要的所有.csv文件),否则会得到ValuError:无效的文件路径或缓冲区对象类型:.

I am writing a Python code using pandas that will open a .csv file and read some parameters that will be used later as input for another module. Along the parameters the code must read, there are locations (paths) of other .csv files in my internal network that contain data that must be incorporated later on to the final output. My problem is opening those files; unless I define explicitly the path (instead of using a reference variable that will allow me to loop over all the .csv files my final code needs), I get the ValuError: Invalid file path or buffer object type: .

我尝试在路径中添加单引号和双引号,但这无济于事.有人可以帮我弄清楚我在做什么错吗?

I tried adding single and double quotation marks to the paths, but that didn't help. Can somebody help me to figure out what I am doing wrong?

下面是我的代码,希望可以帮助您澄清问题.

Below are pieces of my code that hopefuly will help to clarify the issue.

提前感谢您的帮助!

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

我希望我能使自己理解,如果不是这样,请告诉我,我将尝试更详细地解释问题.

I hope I made myself understood, if that's not the case, please let me know and I will try to explain the issue in more detail.

Rgds

Pegaso

推荐答案

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

Not sure yet why, but the problem was in this line of code:

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

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

If I use the method astype(str).Parameters

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

我得到了我想要的结果,只是字符串,让我们看一下:

I get the result I was looking for, just the string, let see it:

之前:

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

...以及当我在末尾添加参数时

...and when I add the parameters at the end

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/

我的问题得到了解决,但是我想更好地理解为什么从数据框中导入数据时为什么会出现这种行为.

I got my problem solved, but I would like to understand better why this behavior when importing data from dataframes.

Rgds

Pegaso

这篇关于在Python中打开路径时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆