Bạn có thể sử dụng phương pháp sau để chuyển đổi tệp văn bản thành tệp CSV bằng Python: Trong phần tiếp theo, bạn sẽ thấy các bước hoàn chỉnh để chuyển đổi tệp văn bản của mình thành CSV. 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ói Pandas trong Windows: Tiếp theo, chụp đường dẫn nơi lưu trữ tệp văn bản trên máy tính của bạn. Dưới đây là một ví dụ về đường dẫn mà tệp văn bản [được gọi là ‘sản phẩm_list,] được lưu trữ: C: \ Users \ Ron \ Desktop \ test \ Product_list.txt Bây giờ, bạn sẽ cần chỉ định đường dẫn nơi tệp CSV mới sẽ được lưu. Ví dụ: C: \ Users \ Ron \ Desktop \ test \ new_products.csv Cuối cùng, bạn có thể sử dụng mẫu bên dưới để tạo điều kiện chuyển đổi tệp văn bản của bạn thành CSV: Ví dụ của chúng tôi: Vì vậy, đây là mã hoàn chỉnh để chuyển đổi tệp văn bản thành CSV cho ví dụ của chúng tôi [lưu ý rằng bạn sẽ cần sửa đổi các đường dẫn để phản ánh vị trí nơi các tệp được lưu trữ trên máy tính của bạn]:[note that you’ll need to modify the paths to reflect the location where the files are
stored on your computer]: Khi bạn chạy mã trong Python [được điều chỉnh theo đường dẫn của bạn], bạn sẽ nhận được tệp CSV tại vị trí được chỉ định của bạn.import pandas as pd
read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt']
read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]
Bước 1: Cài đặt gói Pandas
pip install pandas
Bước 2: Chụp đường dẫn nơi lưu trữ tệp văn bản của bạn
Bước 3: Chỉ định đường dẫn nơi tệp CSV mới sẽ được lưu
Bước 4: Chuyển đổi tệp văn bản thành CSV bằng Python
import pandas as pd
read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt']
read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]
import pandas as pd
read_file = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Product_List.txt']
read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index=None]
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Pandas provide the ExcelWriter class for writing data frame objects to excel sheets.
Syntax:
final = pd.ExcelWriter['GFG.xlsx']
Bàn luận
Sample CSV File:
Python3
Pandas 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. Trong bài viết này, chúng tôi sẽ xử lý việc chuyển đổi tệp .csv thành excel [.xlsx]. & Nbsp; gấu trúc cung cấp lớp Excelwriter để viết các đối tượng khung dữ liệu để Excel Sheets. & NBSP; cú pháp: & nbsp; & nbsp;
Ví dụ: Tệp CSV mẫu: & NBSP;
pip install pandas5
pip install pandas6
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]7
pip install pandas8
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]9
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]1
import pandas as pd read_file = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Product_List.txt'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index=None]1
Output:
pip install pandas
7pip install pandas
8 pip install pandas
9import pandas as pd
read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt']
read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]
0import pandas as pd
read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt']
read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]
1
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]2
pip install pandas8
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]4
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]5
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]1read_* functions are used to read data to pandas, the to_* methods are used to store data. The to_excel[] method stores the data as an excel file. In the example here, the sheet_name is named passengers instead of the default Sheet1. By setting index=False the row index labels are not saved in the spreadsheet.
Python3
Pandas 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. Trong bài viết này, chúng tôi sẽ xử lý việc chuyển đổi tệp .csv thành excel [.xlsx]. & Nbsp; gấu trúc cung cấp lớp Excelwriter để viết các đối tượng khung dữ liệu để Excel Sheets. & NBSP; cú pháp: & nbsp; & nbsp;
Ví dụ: Tệp CSV mẫu: & NBSP;
import pandas as pd read_file = pd.read_csv [r'C:\Users\Ron\Desktop\Test\Product_List.txt'] read_file.to_csv [r'C:\Users\Ron\Desktop\Test\New_Products.csv', index=None]9
final = pd.ExcelWriter['GFG.xlsx']0
final = pd.ExcelWriter['GFG.xlsx']1
pip install pandas8
final = pd.ExcelWriter['GFG.xlsx']3
final = pd.ExcelWriter['GFG.xlsx']4
pip install pandas8
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]9
import pandas as pd read_file = pd.read_csv [r'Path where the Text file is stored\File name.txt'] read_file.to_csv [r'Path where the CSV will be saved\File name.csv', index=None]1
54
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Trong bài đăng này, có một ví dụ Python để chuyển đổi từ CSV sang XLS.
Tuy nhiên, tệp của tôi có hơn 65536 hàng nên XLS không hoạt động. Nếu tôi đặt tên cho tệp xlsx, nó không tạo ra sự khác biệt. Có gói Python để chuyển đổi thành XLSX không?
Đã hỏi ngày 15 tháng 7 năm 2013 lúc 10:21Jul 15, 2013 at 10:21
0
Đây là một ví dụ sử dụng xlsxwriter:
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob[os.path.join['.', '*.csv']]:
workbook = Workbook[csvfile[:-4] + '.xlsx']
worksheet = workbook.add_worksheet[]
with open[csvfile, 'rt', encoding='utf8'] as f:
reader = csv.reader[f]
for r, row in enumerate[reader]:
for c, col in enumerate[row]:
worksheet.write[r, c, col]
workbook.close[]
FYI, cũng có một gói có tên OpenPyXL, có thể đọc/ghi các tệp XLSX/XLSM Excel 2007.
Hy vọng điều đó sẽ giúp.
Đã trả lời ngày 16 tháng 7 năm 2013 lúc 18:51Jul 16, 2013 at 18:51
Alecxealecxealecxe
449K114 Huy hiệu vàng1044 Huy hiệu bạc1167 Huy hiệu đồng114 gold badges1044 silver badges1167 bronze badges
14
Với thư viện của tôi
final = pd.ExcelWriter['GFG.xlsx']8,
$ pip install pyexcel pyexcel-xlsx
Bạn có thể làm điều đó trong một dòng lệnh:
from pyexcel.cookbook import merge_all_to_a_book
# import pyexcel.ext.xlsx # no longer required if you use pyexcel >= 0.2.2
import glob
merge_all_to_a_book[glob.glob["your_csv_directory/*.csv"], "output.xlsx"]
Mỗi CSV sẽ có tờ riêng và tên sẽ là tên tệp của họ.
Đã trả lời ngày 19 tháng 10 năm 2014 lúc 23:42Oct 19, 2014 at 23:42
Chfwchfwchfw
4.4522 Huy hiệu vàng27 Huy hiệu bạc31 Huy hiệu Đồng2 gold badges27 silver badges31 bronze badges
9
Cài đặt OpenPyxL đầu tiên:
pip install openpyxl
Then:
from openpyxl import Workbook
import csv
wb = Workbook[]
ws = wb.active
with open['test.csv', 'r'] as f:
for row in csv.reader[f]:
ws.append[row]
wb.save['name.xlsx']
Paolo
Phim huy hiệu vàng 18,9K2121 gold badges73 silver badges110 bronze badges
Đã trả lời ngày 9 tháng 3 năm 2017 lúc 19:07Mar 9, 2017 at 19:07
Zhuhurenzhuhurenzhuhuren
3173 Huy hiệu bạc7 Huy hiệu đồng3 silver badges7 bronze badges
1
Giải pháp mã hai dòng đơn giản sử dụng gấu trúc
pip install pandas0
Đã trả lời ngày 16 tháng 11 năm 2019 lúc 23:11Nov 16, 2019 at 23:11
Bhanu Sinhabhanu SinhaBhanu Sinha
1.43611 Huy hiệu bạc10 Huy hiệu đồng11 silver badges10 bronze badges
2
Thêm một câu trả lời sử dụng thư viện PANDAS để đọc trong tệp .csv và lưu dưới dạng tệp .xlsx. Ví dụ này sử dụng
final = pd.ExcelWriter['GFG.xlsx']9 [liên kết đến tài liệu] và
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob[os.path.join['.', '*.csv']]:
workbook = Workbook[csvfile[:-4] + '.xlsx']
worksheet = workbook.add_worksheet[]
with open[csvfile, 'rt', encoding='utf8'] as f:
reader = csv.reader[f]
for r, row in enumerate[reader]:
for c, col in enumerate[row]:
worksheet.write[r, c, col]
workbook.close[]
0 [liên kết đến tài liệu].Ví dụ có thể tái tạo hoàn toàn sử dụng Numpy để chỉ tạo các số ngẫu nhiên và điều này có thể được xóa nếu bạn muốn sử dụng tệp .csv của riêng mình.
pip install pandas1
Đã trả lời ngày 29 tháng 12 năm 2017 lúc 17:19Dec 29, 2017 at 17:19
patrickjlong1patrickjlong1patrickjlong1
Phù hiệu vàng 3.5331 Huy hiệu bạc29 Huy hiệu đồng1 gold badge15 silver badges29 bronze badges
2
Chuyển đổi tệp CSV sang XLSX đơn giản 1 đến 1 mà không cần liệt kê/lặp qua các hàng:
pip install pandas2
Notes:
- Tôi đã tìm thấy rằng nếu file_name thực sự dài [> 30 ký tự không bao gồm đường dẫn] thì tệp XLSX kết quả sẽ gây ra lỗi khi Excel cố gắng tải nó. Excel sẽ đề nghị khắc phục lỗi mà nó xảy ra, nhưng nó thật khó chịu.
- Có một câu trả lời tuyệt vời trước đây với điều kiện kết hợp tất cả các tệp CSV trong thư mục thành một sổ làm việc XLSX, phù hợp với trường hợp sử dụng khác hơn là chỉ cố gắng thực hiện tệp CSV 1 trên 1 sang chuyển đổi tệp XLSX.
Đã trả lời ngày 8 tháng 4 năm 2020 lúc 20:16Apr 8, 2020 at 20:16
Larry wlarry wLarry W
811 Huy hiệu bạc5 Huy hiệu đồng1 silver badge5 bronze badges
2
Làm thế nào tôi làm điều đó với OpenPyxl lib:openpyxl lib:
pip install pandas3
McARTON
25,5K5 Huy hiệu vàng76 Huy hiệu bạc85 Huy hiệu Đồng5 gold badges76 silver badges85 bronze badges
Đã trả lời ngày 17 tháng 8 năm 2016 lúc 16:58Aug 17, 2016 at 16:58
RubyConrubyConRubycon
Phim thương hiệu vàng 18K1045 Huy hiệu bạc69 Huy hiệu đồng10 gold badges45 silver badges69 bronze badges
Có một cách đơn giản
pip install pandas4
Đã trả lời ngày 5 tháng 5 năm 2017 lúc 2:23May 5, 2017 at 2:23
David Dingdavid dingDavid Ding
1.3531 Huy hiệu vàng14 Huy hiệu bạc13 Huy hiệu đồng1 gold badge14 silver badges13 bronze badges