Cần chuyển đổi tệp Excel thành tệp CSV bằng Python?
Nếu vậy, bạn có thể sử dụng mẫu sau để chuyển đổi tệp của mình:
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
Và nếu bạn có một tờ Excel cụ thể mà bạn muốn chuyển đổi, thì bạn có thể sử dụng mẫu này:
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
Trong phần tiếp theo, bạn sẽ thấy các bước hoàn chỉnh để chuyển đổi tệp Excel của mình thành tệp CSV bằng Python.
Bước 1: Cài đặt gói Pandas
Nếu bạn đã thực hiện như vậy, hãy cài đặt gói Pandas. Bạn có thể sử dụng lệnh sau để cài đặt gấu trúc [dưới Windows]:
pip install pandas
Bước 2: Chụp đường dẫn nơi lưu trữ tệp Excel
Tiếp theo, chụp đường dẫn nơi lưu trữ tệp Excel trên máy tính của bạn.
Dưới đây là một ví dụ về đường dẫn lưu trữ tệp Excel:
C: \ Users \ Ron \ Desktop \ test \ Product_list.xlsx
Trong đó ‘sản phẩm_list là tên tệp Excel và‘ XLSX là phần mở rộng tệp.Product_List‘ is the Excel file name, and ‘xlsx‘ is the file extension.
Bước 3: Chỉ định đường dẫn nơi tệp CSV mới sẽ được lưu trữ
Bây giờ bạn sẽ cần chỉ định đường dẫn nơi tệp CSV mới sẽ được lưu trữ. & NBSP; ví dụ:
C: \ Users \ Ron \ Desktop \ test \ new_products.csv
Trong đó ‘new_products là tên tệp mới và‘ CSV là phần mở rộng tệp.New_Products‘ is the new file name, and ‘csv‘ is the file extension.
Bước 4: Chuyển đổi Excel thành CSV bằng Python
Đối với phần cuối cùng, hãy sử dụng mẫu sau để hỗ trợ bạn chuyển đổi Excel sang CSV:
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
Đây là cách mã trông giống như trong bối cảnh ví dụ của chúng tôi [bạn sẽ cần sửa đổi các đường dẫn để phản ánh vị trí nơi các tệp sẽ được lưu trữ trên máy tính của bạn]:
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]
Khi bạn chạy mã [điều chỉnh theo đường dẫn của bạn], bạn sẽ nhận được tệp CSV mới tại vị trí được chỉ định của bạn.
Bạn cũng có thể muốn kiểm tra nguồn sau đây cho các bước để chuyển đổi CSV thành Excel bằng Python.
df
=
import pandas as pd
read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx']
read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
5import
7import pandas as pd
read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name']
read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
1
- Phương pháp 3: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện OpenPyXL và CSV.
- OpenPyXL là một thư viện để đọc/viết các tệp excel 2010 xlsx/xlsm/xltx/xltm. Nó được sinh ra từ việc thiếu thư viện hiện có để đọc/viết tự nhiên từ định dạng xml Office Open Open Open.
import
=
0
=
5=
=
7
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]0
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]9here.
Python3
import
pandas as pd
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]5
=
pd.DataFrame[pd.read_excel[
2import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]2
import
2=
import
4import
55556pd.DataFrame[pd.read_excel[
8pip install pandas
7
pandas as pd
6 import pandas as pd
read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name']
read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
08pandas as pd
8 import pandas as pd
read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name']
read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]
10
Phương pháp 1: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện Pandas.
Pandas là một thư viện phần mềm nguồn mở được xây dựng để thao tác và phân tích dữ liệu cho ngôn ngữ lập trình Python. Nó cung cấp các chức năng khác nhau về cấu trúc dữ liệu và hoạt động để thao tác các bảng số và chuỗi thời gian. Nó có thể đọc, lọc và sắp xếp lại các bộ dữ liệu nhỏ và lớn và xuất chúng theo một loạt các định dạng bao gồm Excel, JSON, CSV. is an open-source software library built for data manipulation and analysis for Python programming language. It offers various functionality in terms of data structures and operations for manipulating numerical tables and time series. It can read, filter, and re-arrange small and large datasets and output them in a range of formats including Excel, JSON, CSV.
Để đọc tệp excel, sử dụng phương thức read_excel [] và chuyển đổi khung dữ liệu thành tệp CSV, hãy sử dụng phương thức gấu trúc to_csv [] của gấu trúc.the read_excel[] method and convert the data frame into the CSV file, use to_csv[] method of pandas.
Code:
Python3
import
pandas as pd
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]5
=
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]7
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]0
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]9
pip install pandas0
pip install pandas1
pip install pandas2
pip install pandas3
pip install pandas4
=
pip install pandas6
pip install pandas7
pip install pandas3
pip install pandas9
=
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]1
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]9
df
=
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]5
pip install pandas1
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]1
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]2
Output: Output:
Phương pháp 2: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện XLRD và CSV.
XLRD là một thư viện có mục đích chính để đọc tệp Excel. & NBSP;is a library with the main purpose to read an excel file.
CSV là một thư viện có mục đích chính để đọc và viết tệp CSV. is a library with the main purpose to read and write a csv file.
Code:
Python3
import
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]0
import
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]2
import
pandas as pd
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]5
=
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]7
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]0
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]9
import
0import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]9
import
2=
import
4import
55556import
7
pip install pandas2
import
9pandas as pd
0
pip install pandas2
import
9pandas as pd
3=
pandas as pd
5
pandas as pd
6 pandas as pd
7pandas as pd
8 pandas as pd
9df
0
df
1df
2
df
=
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]5
import
7import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]1
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]2
Output: Output:
Phương pháp 3: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện OpenPyXL và CSV.
OpenPyXL là một thư viện để đọc/viết các tệp excel 2010 xlsx/xlsm/xltx/xltm. Nó được sinh ra từ việc thiếu thư viện hiện có để đọc/viết tự nhiên từ định dạng xml Office Open Open Open. is a library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.It was born from lack of existing library to read/write natively from Python the Office Open XML format.
Code:
Python3
import
=
0
import
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]2
import
pandas as pd
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]5
=
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]7
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]0
import pandas as pd read_file = pd.read_excel [r'C:\Users\Ron\Desktop\Test\Product_List.xlsx'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index = None, header=True]9
import
0import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]9
import
2=
import
4import
55556import
7
pip install pandas2
pandas as pd
6 pandas as pd
7pandas as pd
8 pandas as pd
9df
0
import
9pandas as pd
0
pip install pandas2
import
9pandas as pd
3=
pandas as pd
5
df
=
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]5
import
7import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]1
Phương pháp 3: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện OpenPyXL và CSV.
OpenPyXL là một thư viện để đọc/viết các tệp excel 2010 xlsx/xlsm/xltx/xltm. Nó được sinh ra từ việc thiếu thư viện hiện có để đọc/viết tự nhiên từ định dạng xml Office Open Open Open.
import pandas as pd read_file = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='Your Excel sheet name'] read_file.to_csv [r'Path to store the CSV file\File name.csv', index = None, header=True]2
Output: Output: