gpt4 book ai didi

python - 用于在python中连接数据excel数据的循环文件名?

转载 作者:行者123 更新时间:2023-12-01 01:14:49 25 4
gpt4 key购买 nike

我有一些用于连接 Excel 文件的代码,如何编辑我的代码以便不必每次都键入完整的文件名?

谢谢

代码如下:

import pandas as pd

excel_names = ["Market_Information_System_Control_daily_trading_day_170701.xlsx",
"Market_Information_System_Control_daily_trading_day_170702.xlsx",
"Market_Information_System_Control_daily_trading_day_170703.xlsx",
"Market_Information_System_Control_daily_trading_day_170704.xlsx",
"Market_Information_System_Control_daily_trading_day_170731.xlsx"]

excels = [pd.ExcelFile(name) for name in excel_names]
frames = [x.parse(x.sheet_names[1], header=None,index_col=None) for x in excels]

frames[1:] = [df[1:] for df in frames[1:]]
combined = pd.concat(frames)
combined.to_excel("c.xlsx", header=False, index=False)

编辑1: image

我只是想要时间流动,而不是有标题中断。

我认为这与此有关:frames[1:] = [df[1:] for df inframes[1:]]

最佳答案

这是您要找的吗?这将打印出所有文件名,您只需循环遍历文件名然后组合

from os import listdir
from os.path import isfile, join

folder_path = './'

onlyfiles = [f for f in listdir(folder_path) if isfile(join(folder_path, f))]
print(onlyfiles);

附注这是来自How do I list all files of a directory?

编辑,如果您确实想要进行某种文件扩展名过滤,我使用 json 文件作为示例,请将其更改为您的需要

from os import listdir
from os.path import isfile, join

folder_path = './'

onlyfiles = [f for f in listdir(folder_path) if isfile(join(folder_path, f))]
filter_extension = []
my_ext = 'json'

for f in listdir(folder_path):
extension = f.split('.')[-1]
if (extension == my_ext):
filter_extension.extend([f])

print(filter_extension)

关于python - 用于在python中连接数据excel数据的循环文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54451351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com