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]
Làm cách nào để chuyển đổi tệp TXT thành CSV?
Chuyển đổi tệp văn bản thành CSV cho PC, đi đến menu "Tệp" và chọn "Lưu dưới dạng". Bạn sẽ nhập tên tệp của mình và thêm tiện ích mở rộng ". CSV". Điều này sẽ tự động thay đổi nó thành định dạng CSV!
Bạn có thể lưu tệp văn bản dưới dạng CSV không?
Làm cách nào để chuyển đổi tệp TXT thành CSV?
Chuyển đổi tệp văn bản thành CSV cho PC, đi đến menu "Tệp" và chọn "Lưu dưới dạng". Bạn sẽ nhập tên tệp của mình và thêm tiện ích mở rộng ". CSV". Điều này sẽ tự động thay đổi nó thành định dạng CSV!
Bạn có thể lưu tệp văn bản dưới dạng CSV không?
Chuyển đến Tệp> Lưu dưới dạng. Nhấp vào Duyệt. Trong hộp thoại Lưu dưới dạng, trong Save As Type, chọn định dạng tệp văn bản cho bảng tính; Ví dụ: nhấp vào Text [Tab Delimited] hoặc CSV [dấu hiệu dấu hiệu].
Làm cách nào để chuyển đổi TXT thành DataFrame trong Python?
Chuyển đổi tệp văn bản thành DataFrame.
Tệp CSV được hình thành từ tệp văn bản đã cho
Pandas.Read_FWF - Đọc một bảng các dòng được định dạng có chiều rộng cố định vào DataFrame. pandas.read_fwf [filepath_or_buffer, colspecs = 'infress', widths = none, ** kwds].The first column in dataframe is indexing which is by default when a text file is read.
gấu trúc. read_csv - đọc tệp CSV [được phân tách bằng dấu phẩy] vào DataFrame ..Dataframe.to_csv[] Method.
Xem thảo luận Dataframe.to_csv[parameters]
Cải thiện bài viếtNone
Lưu bài viết
Đọc
Python3
import
pandas as pd
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
=
pd.read_csv[
"GeeksforGeeks.txt"
pip install pandas1
pip install pandas2
pip install pandas3
pip install pandas4
pip install pandas5
=
pip install pandas7
pip install pandas0
Output:
Tệp CSV được hình thành từ tệp văn bản đã cho
Tệp văn bản đọc giống như trên. Sau khi chạy thành công mã trên, một tệp có tên là Geekforgeeks.csv, sẽ được tạo trong cùng một thư mục. & NBSP;
Ví dụ 2: Giả sử tiêu đề cột không được đưa ra và tệp văn bản trông giống như:Suppose the column heading are not given and the text file looks like:
Tệp văn bản không có tiêu đề
Sau đó, trong khi viết mã, bạn có thể chỉ định các tiêu đề. & NBSP;
Python3
import
pandas as pd
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
=
pd.read_csv[
"GeeksforGeeks.txt"
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]6
=
pip install pandas7
pip install pandas0
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]0____7
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]2
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]3
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]4
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]5
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]4
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]7
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]8
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
pip install pandas2
pip install pandas3
import
2
pip install pandas5
=
pip install pandas7
pip install pandas0
Output:Output:
Tệp CSV có tiêu đề
Chúng tôi thấy rằng các tiêu đề đã được thêm thành công và tệp đã được chuyển đổi từ định dạng ‘.txt, sang định dạng .csv.
Ví dụ 3: Trong ví dụ này, các trường trong tệp văn bản được phân tách bởi DELIMITER do người dùng xác định/Mạnh. In this example, the fields in the text file are separated by user defined delimiter “/”.
Tệp văn bản được phân định
Python3
import
pandas as pd
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
=
pd.read_csv[
"GeeksforGeeks.txt"
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]6
=
pip install pandas7
pip install pandas0
pandas as pd
9
pip install pandas2
pandas as pd
3import 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]0____7
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]2
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]3
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]4
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]5
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]4
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]7
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]8
Output:
Tệp CSV
Trong khi đọc dữ liệu, chúng tôi chỉ định rằng dữ liệu nên được mã hóa bằng cách sử dụng dấu phân cách được chỉ định. Trong trường hợp này '/'.‘/’.