欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)樾?
  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ù)開始讀
  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)文章

  • Python 函數(shù)簡(jiǎn)單易理解版

    Python 函數(shù)簡(jiǎn)單易理解版

    本文將用簡(jiǎn)單易解的描述方法對(duì)Python 函數(shù)做一個(gè)詳情介紹,感興趣的朋友可以參考下文
    2021-08-08
  • Python 使用list和tuple+條件判斷詳解

    Python 使用list和tuple+條件判斷詳解

    這篇文章主要介紹了Python 使用list和tuple+條件判斷詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 利用python在excel里面直接使用sql函數(shù)的方法

    利用python在excel里面直接使用sql函數(shù)的方法

    今天小編就為大家分享一篇利用python在excel里面直接使用sql函數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 最新評(píng)論