python將excel轉(zhuǎn)換為csv的代碼方法總結(jié)
更新時(shí)間:2019年07月03日 09:28:48 投稿:laozhang
在本篇文章里小編給大家分享了關(guān)于python如何將excel轉(zhuǎn)換為csv的實(shí)例方法和代碼內(nèi)容,需要的朋友們學(xué)習(xí)下。
python:如何將excel文件轉(zhuǎn)化成CSV格式
import pandas as pd
data = pd.read_excel('123.xls','Sheet1',index_col=0)
data.to_csv('data.csv',encoding='utf-8')
將Excel文件轉(zhuǎn)為csv文件的python腳本
#!/usr/bin/env python
__author__ = "lrtao2010"
'''
Excel文件轉(zhuǎn)csv文件腳本
需要將該腳本直接放到要轉(zhuǎn)換的Excel文件同級(jí)目錄下
支持xlsx 和 xls 格式
在同級(jí)目錄下生成名為excel_to_csv.csv 的文件,采用UTF-8編碼
'''
import xlrd
import csv
import os
#生成的csv文件名
csv_file_name = 'excel_to_csv.csv'
def get_excel_list():
#獲取Excel文件列表
excel_file_list = []
file_list = os.listdir(os.getcwd())
for file_name in file_list:
if file_name.endswith('xlsx') or file_name.endswith('xls'):
excel_file_list.append(file_name)
return excel_file_list
def get_excel_header(excel_name_for_header):
#獲取表頭,并將表頭全部變?yōu)樾?xiě)
workbook = xlrd.open_workbook(excel_name_for_header)
table = workbook.sheet_by_index(0)
#row_value = table.row_values(0)
row_value = [i.lower() for i in table.row_values(0)]
return row_value
def read_excel(excel_name):
#讀取Excel文件每一行內(nèi)容到一個(gè)列表中
workbook = xlrd.open_workbook(excel_name)
table = workbook.sheet_by_index(0) #讀取第一個(gè)sheet
nrows = table.nrows
ncols = table.ncols
# 跳過(guò)表頭,從第一行數(shù)據(jù)開(kāi)始讀
for rows_read in range(1,nrows):
#每行的所有單元格內(nèi)容組成一個(gè)列表
row_value = []
for cols_read in range(ncols):
#獲取單元格數(shù)據(jù)類型
ctype = table.cell(rows_read, cols_read).ctype
#獲取單元格數(shù)據(jù)
nu_str = table.cell(rows_read, cols_read).value
#判斷返回類型
# 0 empty,1 string, 2 number(都是浮點(diǎn)), 3 date, 4 boolean, 5 error
#是2(浮點(diǎn)數(shù))的要改為int
if ctype == 2:
nu_str = int(nu_str)
row_value.append(nu_str)
yield row_value
def xlsx_to_csv(csv_file_name,row_value):
#生成csv文件
with open(csv_file_name, 'a', encoding='utf-8',newline='') as f: #newline=''不加會(huì)多空行
write = csv.writer(f)
write.writerow(row_value)
if __name__ == '__main__':
#獲取Excel列表
excel_list = get_excel_list()
#獲取Excel表頭并生成csv文件標(biāo)題
xlsx_to_csv(csv_file_name,get_excel_header(excel_list[0]))
#生成csv數(shù)據(jù)內(nèi)容
for excel_name in excel_list:
for row_value in read_excel(excel_name):
xlsx_to_csv(csv_file_name,row_value)
print('Excel文件轉(zhuǎn)csv文件結(jié)束 ')
以上就是2種實(shí)例方法,感謝大家的閱讀和對(duì)腳本之家的支持。
相關(guān)文章
解決python3插入mysql時(shí)內(nèi)容帶有引號(hào)的問(wèn)題
今天小編就為大家分享一篇解決python3插入mysql時(shí)內(nèi)容帶有引號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python抓取移動(dòng)App數(shù)據(jù)使用mitmweb監(jiān)聽(tīng)請(qǐng)求與響應(yīng)
這篇文章主要介紹了Python抓取移動(dòng)App數(shù)據(jù)使用mitmweb監(jiān)聽(tīng)請(qǐng)求與響應(yīng),mitmproxy控制臺(tái)方式、mitmdump與Python對(duì)接的方式、mitmweb可視化方式,需要的朋友可以參考一下2022-01-01
實(shí)例說(shuō)明Python中比較運(yùn)算符的使用
這篇文章主要介紹了=Python中比較運(yùn)算符的使用,是Python學(xué)習(xí)當(dāng)中的基本知識(shí),需要的朋友可以參考下2015-05-05
python中zip和unzip數(shù)據(jù)的方法
這篇文章主要介紹了python中zip和unzip數(shù)據(jù)的方法,實(shí)例分析了Python中zlib模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
Python批量上傳文件信息到服務(wù)器的實(shí)現(xiàn)示例
在進(jìn)行軟件測(cè)試的過(guò)程中,經(jīng)常會(huì)需要準(zhǔn)備一批數(shù)據(jù),本文主要介紹了Python批量上傳文件信息到服務(wù)器的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Python分支結(jié)構(gòu)(switch)操作簡(jiǎn)介
這篇文章主要介紹了Python分支結(jié)構(gòu)(switch)操作簡(jiǎn)介,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01

