在Python中将 Excel 转换为 CSV
在本文中,我们将处理 Excel (.xlsx) 文件到 .csv 的转换。 Excel 中主要使用两种格式:
- (*.xlsx) : Excel Microsoft Office Open XML 格式电子表格文件。
- (*.xls):Excel 电子表格(Excel 97-2003 工作簿)。
让我们考虑一个购物商店的数据集,其中包含存储在 Excel 文件中的有关客户序列号、客户名称、客户 ID 和产品成本的数据。
在此处检查所有使用的文件。
Python3
# importing pandas as pd
import pandas as pd
# read an excel file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_excel("Test.xlsx"))
# show the dataframe
df
Python3
#importing pandas as pd
import pandas as pd
# Read and store content
# of an excel file
read_file = pd.read_excel ("Test.xlsx")
# Write the dataframe object
# into csv file
read_file.to_csv ("Test.csv",
index = None,
header=True)
# read csv file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_csv("Test.csv"))
# show the dataframe
df
Python3
# import all required library
import xlrd
import csv
import pandas as pd
# open workbook by sheet index,
# optional - sheet_by_index()
sheet = xlrd.open_workbook("Test.xlsx").sheet_by_index(0)
# writer object is created
col = csv.writer(open("T.csv",
'w',
newline=""))
# writing the data into csv file
for row in range(sheet.nrows):
# row by row write
# operation is perform
col.writerow(sheet.row_values(row))
# read csv file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_csv("T.csv"))
# show the dataframe
df
Python3
# importe required libraries
import openpyxl
import csv
import pandas as pd
# open given workbook
# and store in excel object
excel = openpyxl.load_workbook("Test.xlsx")
# select the active sheet
sheet = excel.active
# writer object is created
col = csv.writer(open("tt.csv",
'w',
newline=""))
# writing the data in csv file
for r in sheet.rows:
# row by row write
# operation is perform
col.writerow([cell.value for cell in r])
# read the csv file and
# convert into dataframe object
df = pd.DataFrame(pd.read_csv("tt.csv"))
# show the dataframe
df
输出 :
现在,让我们看看将 Excel 文件转换为 CSV 文件的不同方法:
方法 1:使用 pandas 库将 Excel 文件转换为 CSV 文件。
Pandas是一个开源软件库,专为Python编程语言的数据操作和分析而构建。它在数据结构和操作方面提供了各种功能,用于操作数字表和时间序列。它可以读取、过滤和重新排列大小数据集,并以 Excel、JSON、CSV 等多种格式输出。
读取 excel 文件,使用read_excel()方法并将数据框转换为 CSV 文件,使用 pandas 的to_csv()方法。
代码:
Python3
#importing pandas as pd
import pandas as pd
# Read and store content
# of an excel file
read_file = pd.read_excel ("Test.xlsx")
# Write the dataframe object
# into csv file
read_file.to_csv ("Test.csv",
index = None,
header=True)
# read csv file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_csv("Test.csv"))
# show the dataframe
df
输出:
方法 2:使用 xlrd 和 CSV 库将 Excel 文件转换为 CSV 文件。
xlrd是一个主要用于读取 excel 文件的库。
csv是一个主要用于读取和写入 csv 文件的库。
代码:
Python3
# import all required library
import xlrd
import csv
import pandas as pd
# open workbook by sheet index,
# optional - sheet_by_index()
sheet = xlrd.open_workbook("Test.xlsx").sheet_by_index(0)
# writer object is created
col = csv.writer(open("T.csv",
'w',
newline=""))
# writing the data into csv file
for row in range(sheet.nrows):
# row by row write
# operation is perform
col.writerow(sheet.row_values(row))
# read csv file and convert
# into a dataframe object
df = pd.DataFrame(pd.read_csv("T.csv"))
# show the dataframe
df
输出:
方法 3:使用 openpyxl 和 CSV 库将 Excel 文件转换为 CSV 文件。
openpyxl是一个用于读取/写入 Excel 2010 xlsx/xlsm/xltx/xltm 文件的库。它诞生于缺乏从Python本地读取/写入 Office Open XML 格式的现有库。
代码:
Python3
# importe required libraries
import openpyxl
import csv
import pandas as pd
# open given workbook
# and store in excel object
excel = openpyxl.load_workbook("Test.xlsx")
# select the active sheet
sheet = excel.active
# writer object is created
col = csv.writer(open("tt.csv",
'w',
newline=""))
# writing the data in csv file
for r in sheet.rows:
# row by row write
# operation is perform
col.writerow([cell.value for cell in r])
# read the csv file and
# convert into dataframe object
df = pd.DataFrame(pd.read_csv("tt.csv"))
# show the dataframe
df
输出: