新野微网站开发,莱芜论坛哪个比较好,正规游戏代理平台,江西有色建设集团有限公司网站人生苦短我用Python excel转csv 前言准备工作pandas库主要类和方法ExcelFile 类DataFrame 类read_excel 函数to_csv 函数 示例 前言 
Excel 文件和csv文件都是常用的电子表格文件格式#xff0c;其中csv格式更便于用于数据交换和处理。本文使用pandas库将Excel文件转化为csv文… 人生苦短我用Python excel转csv 前言准备工作pandas库主要类和方法ExcelFile 类DataFrame 类read_excel 函数to_csv 函数 示例 前言 
Excel 文件和csv文件都是常用的电子表格文件格式其中csv格式更便于用于数据交换和处理。本文使用pandas库将Excel文件转化为csv文件。 
准备工作 
pip install pandas
pip install openpyxlpandas库 csv库是Python标准库的一部分提供了基本的csv文件读写功能。它不能直接支持读取 Excel 文件。  要读取 Excel 文件通常需要使用 pandas 库。以下是来自官网的介绍  pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language. 在读取不同文件格式所需要的engine。openpyxl 是一个用于读写 Excel 2010 xlsx/xlsm/xltx/xltm 文件的Python库。支持通过 Python 代码创建、修改和读取 Excel 文件而无需依赖于 Microsoft Excel 应用程序。 engine : {{openpyxl, calamine, odf, pyxlsb, xlrd}}, default NoneIf io is not a buffer or path, this must be set to identify io.Engine compatibility :- openpyxl supports newer Excel file formats.- calamine supports Excel (.xls, .xlsx, .xlsm, .xlsb)and OpenDocument (.ods) file formats.- odf supports OpenDocument file formats (.odf, .ods, .odt).- pyxlsb supports Binary Excel files.- xlrd supports old-style Excel files (.xls).When engineNone, the following logic will be used to determine the engine:- If path_or_buffer is an OpenDocument format (.odf, .ods, .odt),then odf https://pypi.org/project/odfpy/_ will be used.- Otherwise if path_or_buffer is an xls format, xlrd will be used.- Otherwise if path_or_buffer is in xlsb format, pyxlsb will be used.- Otherwise openpyxl will be used.主要类和方法 
pandas库中ExcelFile 类主要用于读取Excel文件 DataFrame 类用于表示和操作数据。 
ExcelFile 类 
ExcelFile 类用于处理 Excel 文件封装了解析和读取Excel文件的操作。支持查看 Excel 文件中的工作表名称并读取特定的工作表。支持读取 Excel 文件中的多个工作表并将每个工作表转换为一个 DataFrame 对象。 
class ExcelFile:def __init__(self,path_or_buffer,engine: str | None  None,storage_options: StorageOptions | None  None,engine_kwargs: dict | None  None,) - None:propertydef sheet_names(self):return self._reader.sheet_names 
DataFrame 类 
DataFrame 类用于表示二维的、大小可变、潜在异构的表格数据。可以包含多种数据类型的列如整数、浮点数、字符串等。可以进行各种数据操作如选择、过滤、修改、合并、分组、排序等。 
read_excel 函数 
pandas 库 read_excel 函数用于从 Excel 文件中读取数据并将其转换为 DataFrame 对象。支持多种参数来处理不同的 Excel 文件格式和内容。 
def read_excel(io,sheet_name: str | int | list[IntStrT] | None  0,*,header: int | Sequence[int] | None  0,names: SequenceNotStr[Hashable] | range | None  None,index_col: int | str | Sequence[int] | None  None,usecols: int| str| Sequence[int]| Sequence[str]| Callable[[str], bool]| None  None,dtype: DtypeArg | None  None,engine: Literal[xlrd, openpyxl, odf, pyxlsb, calamine] | None  None,converters: dict[str, Callable] | dict[int, Callable] | None  None,true_values: Iterable[Hashable] | None  None,false_values: Iterable[Hashable] | None  None,skiprows: Sequence[int] | int | Callable[[int], object] | None  None,nrows: int | None  None,na_valuesNone,keep_default_na: bool  True,na_filter: bool  True,verbose: bool  False,parse_dates: list | dict | bool  False,date_parser: Callable | lib.NoDefault  lib.no_default,date_format: dict[Hashable, str] | str | None  None,thousands: str | None  None,decimal: str  .,comment: str | None  None,skipfooter: int  0,storage_options: StorageOptions | None  None,dtype_backend: DtypeBackend | lib.NoDefault  lib.no_default,engine_kwargs: dict | None  None,
) - DataFrame | dict[IntStrT, DataFrame]:常用参数 
参数说明默认值ioExcel 文件的路径或文件对象sheet_name要读取的工作表名称或索引。可以是字符串工作表名称、整数工作表索引、列表多个工作表或 None所有工作表默认为 0第一个工作表header指定哪一行作为列名默认为 0第一行index_col指定哪一列作为行索引。可以是整数或列名usecols指定要读取的列。可以是列索引、列名或列范围。dtype指定列的数据类型。可以是字典键为列名值为数据类型。skiprows跳过文件开头的一些行。可以是整数或列表。nrows要读取的行数。 
to_csv 函数 
DataFrame 对象提供了一个非常方便的方法 to_csv用于将 DataFrame 中的数据写入 CSV 文件。 def to_csv(self,path_or_buf: FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None  None,sep: str  ,,na_rep: str  ,float_format: str | Callable | None  None,columns: Sequence[Hashable] | None  None,header: bool_t | list[str]  True,index: bool_t  True,index_label: IndexLabel | None  None,mode: str  w,encoding: str | None  None,compression: CompressionOptions  infer,quoting: int | None  None,quotechar: str  ,lineterminator: str | None  None,chunksize: int | None  None,date_format: str | None  None,doublequote: bool_t  True,escapechar: str | None  None,decimal: str  .,errors: OpenFileErrors  strict,storage_options: StorageOptions | None  None,) - str | None:常用参数 
参数说明默认值path_or_buf输出文件的路径或文件对象。如果为 None则返回 CSV 字符串。Nonesep分隔符默认为逗号 ,index是否写入行索引默认为 Trueheader是否写入列名默认为 Truecolumns指定要写入的列默认为所有列encoding指定编码格式默认为 utf-8 
示例 
实现很简单 
使用 pandas 库读取 Excel 文件读取工作表并将其转换为 DataFrame 对象将 DataFrame 写入 csv 文件。 
import osimport pandas as pddef export_csv(input_file, output_path):# 创建ExcelFile对象with pd.ExcelFile(input_file) as xls:# 获取工作表名称列表for i, sheet_name in enumerate(xls.sheet_names):# 读取工作表并转换为DataFramedf  pd.read_excel(xls, sheet_namesheet_name)output_file  os.path.join(output_path, f{i  1}-{sheet_name}.csv)# 将DataFrame中的数据写入CSV文件。df.to_csv(output_file, indexFalse) 文章转载自: http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.rfwkn.cn.gov.cn.rfwkn.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.jfch.cn.gov.cn.jfch.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.kwfnt.cn.gov.cn.kwfnt.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.tsynj.cn.gov.cn.tsynj.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.kyflr.cn.gov.cn.kyflr.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.kbyp.cn.gov.cn.kbyp.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.sgrwd.cn.gov.cn.sgrwd.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.kgphd.cn.gov.cn.kgphd.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.psdbf.cn.gov.cn.psdbf.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.xjmyq.com.gov.cn.xjmyq.com http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.lhqw.cn.gov.cn.lhqw.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.wmmtl.cn.gov.cn.wmmtl.cn http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.ndynz.cn.gov.cn.ndynz.cn http://www.morning.ppqjh.cn.gov.cn.ppqjh.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.yrnrr.cn.gov.cn.yrnrr.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn