如何使用PANDA和GLOBE将多个.xlsx文件转换为.csv

2022-05-30 00:00:00 python pandas glob

问题描述

我有一个包含多个.xlsx文件的文件夹JanuaryDataSentToResourcePro。 我想遍历文件夹并将它们全部转换为.csv并保持相同的文件名。

为此,我正在尝试实现GLOB,但收到错误:TypeError: 'module' object is not callable

import glob
excel_files = glob('*xlsx*')

for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(r'''C:UsersusernameDocumentsTestFolderJanuaryDataSentToResourceProResourceProDailyDataset_01_01_2018.xlsx''', 'ResourceProDailyDataset')
    df.to_csv(out) 

我是新手。看起来对吗?

更新:

import pandas as pd
import glob
excel_files = glob.glob("*.xlsx")

for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel, 'ResourceProDailyDataset')
    df.to_csv(out)

但仍未将.xlsx转换为.csv


解决方案

GLOB包的使用方法如下:

import glob
f = glob.glob("*.xlsx")

GLOB不是方法,但GLOB.GLOB是。

========================================

import glob
excel_files = glob.glob('C:/Users/username/Documents/TestFolder/JanuaryDataSentToResourcePro/*.xlsx') # assume the path
for excel in excel_files:
    out = excel.split('.')[0]+'.csv'
    df = pd.read_excel(excel) # if only the first sheet is needed.
    df.to_csv(out) 

相关文章