批量csv转xlsx代码

罗子僧

关注

阅读 35

2022-04-14

# 模块导入
import os
from pandas import read_csv

# 输入csv文件们 所在的 文件夹
file_path = "F:/data/hhy/hhy_NDVI/hhy/Zonal/permafrost/"

# 输出的xlsx文件们 所在的 文件夹
out_path = "F:/data/hhy/hhy_NDVI/hhy/Zonal/permafrost/excel/"

# 输入文件的文件名后缀
ext_name = "csv"

# 遍历输入文件夹下的所有文件
for in_file in os.listdir(file_path):
    # 如果文件名后缀符合csv
    if in_file.endswith(ext_name):
        f_name = os.path.splitext(in_file)
        print(f_name)
        
        path = file_path + in_file
        file = open(path)
        data = read_csv(file)
        
        # 输出为xlsx
        data.to_excel(out_path + f_name[0] + ".xlsx")
        
print("finish")

精彩评论(0)

0 0 举报