Hướng dẫn how do i convert xlsx to csv in python? - làm cách nào để chuyển đổi xlsx sang csv trong python?

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]
5import7
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

  1. Phương pháp 3: Chuyển đổi tệp Excel sang tệp CSV bằng thư viện OpenPyXL và CSV.
  2. 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[2

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

import2= import4import55556pd.DataFrame[pd.read_excel[8

pip install pandas
7

pandas as pd6

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 pd8
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 pandas
0
pip install pandas
1
pip install pandas
2

pip install pandas
3
pip install pandas
4=
pip install pandas
6
pip install pandas
7

pip install pandas
3
pip install pandas
9=
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 pandas
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]
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]
9import0
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

import2= import4import55556import7

pip install pandas
2

import9pandas as pd0

pip install pandas
2

import9pandas as pd3=pandas as pd5

pandas as pd6 pandas as pd7pandas as pd8 pandas as pd9df 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]
5import7
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 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]
9import0
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

import2= import4import55556import7

pip install pandas
2

pandas as pd6 pandas as pd7pandas as pd8 pandas as pd9df 0

import9pandas as pd0

pip install pandas
2

import9pandas as pd3=pandas as pd5

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]
5import7
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

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: 


import =0

Để đọ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.use to_csv[] method of pandas.

Bạn có thể mở XLSX dưới dạng CSV không?

Làm thế nào để chuyển đổi XLSX thành tệp CSV?Chọn tệp XLSX mà bạn muốn chuyển đổi.Select CSV làm định dạng bạn muốn chuyển đổi tệp XLSX của mình thành. Bấm "Chuyển đổi" để chuyển đổi tệp XLSX của bạn.Choose the XLSX file that you want to convert. Select CSV as the the format you want to convert your XLSX file to. Click "Convert" to convert your XLSX file.

Chúng ta có thể đọc tệp XLSX trong Python không?

OpenPyXL là một thư viện Python được tạo để đọc và viết các tệp Excel 2010 XLSX/XLSM/XLTX/XLTM.Nó có thể đọc cả hai.XLSX và.Các định dạng tệp XLSM, bao gồm hỗ trợ biểu đồ, biểu đồ và trực quan hóa dữ liệu khác.. It can read both the . xlsx and . xlsm file formats, which includes support for charts, graphs, and other data visualizations.

Bài Viết Liên Quan

Chủ Đề