阿里云建公司網(wǎng)站百度投票人氣排行榜入口
人生苦短我用Python excel轉(zhuǎn)csv
- 前言
- 準(zhǔn)備工作
- pandas庫
- 主要類和方法
- ExcelFile 類
- DataFrame 類
- read_excel 函數(shù)
- to_csv 函數(shù)
- 示例
前言
Excel
文件和csv
文件都是常用的電子表格文件格式,其中csv
格式更便于用于數(shù)據(jù)交換和處理。本文使用pandas
庫將Excel
文件轉(zhuǎn)化為csv
文件。
準(zhǔn)備工作
pip install pandas
pip install openpyxl
pandas庫
-
csv
庫是Python
標(biāo)準(zhǔn)庫的一部分,提供了基本的csv
文件讀寫功能。它不能直接支持讀取Excel
文件。 -
要讀取
Excel
文件,通常需要使用pandas
庫。以下是來自官網(wǎng)的介紹:
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
是一個(gè)用于讀寫 Excel 2010 xlsx/xlsm/xltx/xltm 文件的Python
庫。支持通過Python
代碼創(chuàng)建、修改和讀取Excel
文件,而無需依賴于 Microsoft Excel 應(yīng)用程序。
"""
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 ``engine=None``, 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
類用于表示和操作數(shù)據(jù)。
ExcelFile 類
ExcelFile
類用于處理Excel
文件,封裝了解析和讀取Excel
文件的操作。- 支持查看
Excel
文件中的工作表名稱,并讀取特定的工作表。 - 支持讀取
Excel
文件中的多個(gè)工作表,并將每個(gè)工作表轉(zhuǎn)換為一個(gè)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
類用于表示二維的、大小可變、潛在異構(gòu)的表格數(shù)據(jù)。- 可以包含多種數(shù)據(jù)類型的列,如整數(shù)、浮點(diǎn)數(shù)、字符串等。
- 可以進(jìn)行各種數(shù)據(jù)操作,如選擇、過濾、修改、合并、分組、排序等。
read_excel 函數(shù)
pandas
庫read_excel
函數(shù),用于從 Excel 文件中讀取數(shù)據(jù)并將其轉(zhuǎn)換為DataFrame
對象。- 支持多種參數(shù)來處理不同的 Excel 文件格式和內(nèi)容。
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_values=None,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]:
- 常用參數(shù)
參數(shù) | 說明 | 默認(rèn)值 |
---|---|---|
io | Excel 文件的路徑或文件對象 | |
sheet_name | 要讀取的工作表名稱或索引。 可以是字符串(工作表名稱)、整數(shù)(工作表索引)、列表(多個(gè)工作表)或 None (所有工作表) | 默認(rèn)為 0 (第一個(gè)工作表) |
header | 指定哪一行作為列名 | 默認(rèn)為 0 (第一行) |
index_col | 指定哪一列作為行索引??梢允钦麛?shù)或列名 | |
usecols | 指定要讀取的列。可以是列索引、列名或列范圍。 | |
dtype | 指定列的數(shù)據(jù)類型??梢允亲值?#xff0c;鍵為列名,值為數(shù)據(jù)類型。 | |
skiprows | 跳過文件開頭的一些行??梢允钦麛?shù)或列表。 | |
nrows | 要讀取的行數(shù)。 |
to_csv 函數(shù)
DataFrame
對象提供了一個(gè)非常方便的方法to_csv
,用于將DataFrame
中的數(shù)據(jù)寫入 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:
- 常用參數(shù)
參數(shù) | 說明 | 默認(rèn)值 |
---|---|---|
path_or_buf | 輸出文件的路徑或文件對象。 如果為 None ,則返回 CSV 字符串。 | None |
sep | 分隔符 | 默認(rèn)為逗號 , |
index | 是否寫入行索引 | 默認(rèn)為 True |
header | 是否寫入列名 | 默認(rèn)為 True |
columns | 指定要寫入的列 | 默認(rèn)為所有列 |
encoding | 指定編碼格式 | 默認(rèn)為 utf-8 |
示例
實(shí)現(xiàn)很簡單:
- 使用
pandas
庫讀取Excel
文件; - 讀取工作表并將其轉(zhuǎn)換為
DataFrame
對象; - 將
DataFrame
寫入csv
文件。
import osimport pandas as pddef export_csv(input_file, output_path):# 創(chuàng)建ExcelFile對象with pd.ExcelFile(input_file) as xls:# 獲取工作表名稱列表for i, sheet_name in enumerate(xls.sheet_names):# 讀取工作表并轉(zhuǎn)換為DataFramedf = pd.read_excel(xls, sheet_name=sheet_name)output_file = os.path.join(output_path, f'{i + 1}-{sheet_name}.csv')# 將DataFrame中的數(shù)據(jù)寫入CSV文件。df.to_csv(output_file, index=False)