从.config文件获取路径时出现Python Pandas数据帧错误

2024-05-16 15:03:33 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用(.config)文件创建数据帧以获取该文件,但在从以下文件创建数据帧期间出错

实际文件名:rgf_ltd_060520202

my config fil(管道分离)的示例结构:

...|/user/Doc/ABC/rgf_ltd_[0-9]*|CSV|Collection

从这里开始,当我试图通过在脚本中获取配置文件来创建数据帧时

import pandas as pd

#fetching details fromconfig file
with open('config','r') as rd:
   lines=rd.readlies() 
   for line in lines:
       f_path=#fetching my csv file path(/user/Doc/ABC/rgf_ltd_[0-9]*)

当我在read\u csv函数中传递f\u路径时,python脚本也会获取上面的部分工作文件和/user/Doc/ABC/rgf\u ltd\u0-9]*

#dataframe 
data=pd.read_csv(f_path,sep='|',engine='python')

当我执行上述脚本解释器时,会抛出一个错误:

No such file or Directory:/user/Doc/ABC/rgf_ltd_[0-9]*

我使用这个正则表达式是为了使我的路径更加动态


Tags: csv数据path脚本configdocmyas
1条回答
网友
1楼 · 发布于 2024-05-16 15:03:33

pandas.read_csv在文件读取时不处理regex模式,您可以使用pythonglob.glob模块获得类似的shell样式通配符结果

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools//.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell). Whether or not the results are sorted depends on the file system.

import glob
import os
import pandas as pd

f_path = os.path.join("user","Doc", "ABC")
f_pattern = "rgf_ltd_[0-9]*"

file_list = glob.glob(os.path.join(f_path, f_pattern))
print(file_list) # ['user\\Doc\\ABC\\rgf_ltd_3498543058']

# dataframe
data = pd.read_csv(file_list[0], sep='|', engine='python')
print(data)

来自数据的输出

             Col0            Col1            Col2
0  InsideRGF_R0C0  InsideRGF_R0C1  InsideRGF_R0C2
1  InsideRGF_R1C0  InsideRGF_R1C1  InsideRGF_R1C2
2  InsideRGF_R2C0  InsideRGF_R2C1  InsideRGF_R2C2
3  InsideRGF_R3C0  InsideRGF_R3C1  InsideRGF_R3C2

相关问题 更多 >