- 代码与解释如下:
# -*- coding: utf-8 -*-
import pandas as pd
import os
Folder_Path= 'E:/Lachnospira eligens-all/GCMS_L.e All/Overall_SCFA_SPME' # target files's location
SaveFile_Path='E:/Lachnospira eligens-all/GCMS_L.e All' # Output path
SaveFile_Name='SCFA_SPME_Merged_all.csv' # Output file name
os.chdir(Folder_Path)
file_list = os.listdir()
# 遍历读取文件夹中的每一个文件
fileStart = 1
fileEnd = len(file_list)
for i in range(fileStart,fileEnd):
df = pd.read_csv(Folder_Path + '/' + file_list[i],
encoding = "utf8")
df.to_csv (SaveFile_Path + '/' + SaveFile_Name,
encoding = "utf8",
index = False,
header = True,#如果对每个文件的标题另外有需求,需要对header变量进行指定
mode = 'a')
print('Hey Man! I Just Merged %i CSV Files'% len(file_list))
即可看到合并好的文件和输出:
网友评论