1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import os import pandas as pd folder_path = 'E:/data/csv_path'
dfs_dict = {} for filename in os.listdir(folder_path): if filename.endswith('.csv'): file_path = os.path.join(folder_path, filename) try: df = pd.read_csv(file_path, encoding='utf-8') dfs_dict[filename] = df except UnicodeDecodeError: try: df = pd.read_csv(file_path, encoding='gbk') dfs_dict[filename] = df except Exception as e: print(f"无法读取文件 {file_path},错误:{e}")
for filename, df in dfs_dict.items(): print(f"{filename}") print(df.head()) print("-" * 50)
filenames = sorted(dfs_dict.keys())
input = dfs_dict[filenames[3]].iloc[:,0:6] input
|