国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

阿里云建公司網(wǎng)站百度投票人氣排行榜入口

阿里云建公司網(wǎng)站,百度投票人氣排行榜入口,貴州省建設(shè)部網(wǎng)站,網(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文…

人生苦短我用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ù)

  • pandasread_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)值
ioExcel 文件的路徑或文件對象
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)
http://m.aloenet.com.cn/news/42938.html

相關(guān)文章:

  • 溫州網(wǎng)站建設(shè)溫州網(wǎng)站制作抖音seo運(yùn)營模式
  • 貴州省房屋和城市建設(shè)廳官方網(wǎng)站南京做網(wǎng)站的公司
  • 網(wǎng)站空間是虛擬主機(jī)嗎網(wǎng)站關(guān)鍵詞怎樣優(yōu)化
  • 酒吧營銷用什么軟件找客源西安關(guān)鍵詞優(yōu)化平臺
  • 網(wǎng)站開發(fā)終止協(xié)議書工具大全
  • 圖書館門戶網(wǎng)站建設(shè)的意義重慶森林講的什么內(nèi)容
  • 網(wǎng)站怎么做訪問日志搜索指數(shù)查詢平臺
  • 濟(jì)南網(wǎng)站外包友鏈查詢站長工具
  • 武漢大型網(wǎng)站建設(shè)哪里有學(xué)市場營銷培訓(xùn)班
  • 趙縣網(wǎng)站建設(shè)公司seo jsbapp9
  • 找個(gè)做網(wǎng)站的人培訓(xùn)機(jī)構(gòu)還能開嗎
  • 杭州微信網(wǎng)站建設(shè)企業(yè)網(wǎng)站的搜索引擎推廣與優(yōu)化
  • 石家莊市高新區(qū)建設(shè)局網(wǎng)站百度競價(jià)推廣后臺
  • 都有哪些電商平臺優(yōu)化seo軟件
  • 攝影網(wǎng)站的模板會(huì)計(jì)培訓(xùn)機(jī)構(gòu)
  • 美食網(wǎng)站開發(fā)方案百度搜索結(jié)果優(yōu)化
  • 專業(yè)的網(wǎng)站建設(shè)公司免費(fèi)平臺
  • 做當(dāng)?shù)亻T戶網(wǎng)站多少錢app推廣方案范例
  • 一個(gè)人做網(wǎng)站好做嗎推廣效果最好的平臺
  • 網(wǎng)站icp備案流程現(xiàn)在比較好的營銷平臺
  • 企業(yè)加盟網(wǎng)站建設(shè)北京seo營銷培訓(xùn)
  • 個(gè)人網(wǎng)站建設(shè)詳細(xì)教程百度seo關(guān)鍵詞優(yōu)化方案
  • 安陽網(wǎng)站如何做優(yōu)化營銷推廣公司案例
  • 動(dòng)態(tài)網(wǎng)站開發(fā)的集成網(wǎng)站網(wǎng)絡(luò)營銷是做什么的
  • 社群電商平臺排名搜索引擎優(yōu)化的內(nèi)部優(yōu)化
  • 公司頁面設(shè)計(jì)圖片寧波seo網(wǎng)絡(luò)推廣咨詢價(jià)格
  • wordpress 網(wǎng)站備案號搜索引擎營銷的主要方法包括
  • 黨務(wù)網(wǎng)站建設(shè)總體要求東莞海外網(wǎng)絡(luò)推廣
  • 平面設(shè)計(jì)資源網(wǎng)站微信搜索seo優(yōu)化
  • 哈爾濱暫?,F(xiàn)場業(yè)務(wù)內(nèi)蒙古seo