我有一个功能,可以清理列表中的csv文件:
fileListLocalDaily = (glob.glob("/path/to/my/directory/*.csv")
for y in fileListLocalDaily:
data = y
def prepare_file(y):
data = y
lines = pd.read_csv(data, sep=",", quoting=csv.QUOTE_NONE)
new_lines = lines.replace('something', '', regex=True)
f = StringIO(data)
# extract date
date = next(f)
date = date.split('_')[1]
date = os.path.splitext(date)[0]
new_lines.insert(2,'date',date)
new_lines.drop(new_lines.columns[0:2], axis=1, inplace=True)
new_lines.drop(new_lines.columns[6], axis=1, inplace=True)
new_lines=new_lines.sort_values(by=['Something'], ascending=False)
new_lines.to_csv('/path/to/my/output/'+date+'.csv', index = False)
complete = prepare_file(data)
runFunction = prepare_file(y)
似乎上面的函数只保存了一个文件,并在一个无休止的循环中不断地覆盖它。有人能帮助我理解我如何一个接一个地对目录中的所有csv文件运行此功能吗?谢谢
根据您提供的代码,您的循环实际上没有做任何事情。您一次又一次地定义函数,但没有在循环中调用它,因为最后一行的缩进在循环之外。你的函数在最后也调用了它自己,所以它进入了一个无限循环。您应该定义函数一次,然后在循环内部调用它:
prepare_file
没有返回任何内容,因此赋值操作符只是在那里赋值None
,所以我删除了赋值。我还将循环和函数中的y
直接重命名为data
我喜欢使用os.walk递归地获取所有文件
相关问题 更多 >
编程相关推荐