Hướng dẫn how do i convert csv data to text in python? - làm cách nào để chuyển đổi dữ liệu csv thành văn bản trong python?

Thử thách cơ bản

💬 Thử thách: Làm thế nào để chuyển đổi tệp CSV thành tệp văn bản trong Python?Challenge: How to convert a CSV file to a text file in Python?

Tại đây, nội dung của một tệp CSV ví dụ

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
3 được sử dụng trong đoạn mã của chúng tôi bên dưới:

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

Nếu bạn trực quan hóa CSV này ở dạng bảng, có vẻ như sau:

TênNghề nghiệpTuổi tácThu nhập = earnings
AliceLập trình viên23 110000
BobChấp hành, quản lý34 90000
CarlViệc bán hàng45 50000

Vấn đề cơ bản là chuyển đổi tệp CSV

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
3 thành tệp TXT mới
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
5 như là không thay đổi nội dung của nó

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

Chúng tôi bắt đầu với việc khám phá thách thức cơ bản này và xây dựng từ đó bằng cách thay đổi dấu phân cách và sử dụng gấu trúc để truy cập các cột riêng lẻ.

Nhưng điều đầu tiên trước tiên: Làm thế nào để chuyển đổi tệp CSV thành tệp TXT mà không thay đổi nội dung của nó?How to convert a CSV file to a TXT file without changing its contents?

Phương pháp 1: CSV đến TXT không thay đổi

Nếu bạn muốn giữ nội dung (bao gồm cả dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6) trong tệp CSV không được sửa đổi, việc chuyển đổi rất đơn giản: Đọc tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và ghi nội dung của nó vào tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 mới .

Nói cách khác, thực hiện ba bước để viết CSV vào tệp TXT không được sửa đổi:

  1. Mở tệp CSV ở chế độ đọc và tệp TXT ở chế độ viết.
  2. Đọc tệp CSV và lưu trữ nó trong một biến.
  3. Viết nội dung vào tệp TXT.

Tại đây, đoạn mã mã giải quyết thách thức cơ bản của chúng tôi:

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)

Thực tế ít được biết đến: Python cho phép nhiều biểu thức trong trình quản lý ngữ cảnh (dòng mở ____22) nếu bạn tách chúng bằng dấu phẩy.Little-Known Fact: Python allows multiple expressions in the context manager (

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
2 opening line) if you separate them with a comma.

Nội dung của các tệp

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 giống hệt nhau:

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

Càng xa càng tốt. Nhưng điều gì sẽ xảy ra nếu bạn có một vấn đề hơi khác nhau:

Phương pháp 2: Phân định không gian trống CSV đến TXT

Thử thách: Làm thế nào để chuyển đổi tệp CSV thành tệp TXT trong Python bằng cách thay thế dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 bằng khoảng trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6?
: How to convert a CSV file to a TXT file in Python by replacing the delimiter
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 with the empty space
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6?

Ví dụ: Chuyển đổi tệp sau ________ 27: Convert the following file

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
7…

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000

Tập tin này

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
8

Name Job Age Income
Alice Programmer 23 110000
Bob Executive 34 90000
Carl Sales 45 50000

Ở đây, giải pháp đơn giản cho thử thách này:

Nếu bạn muốn thay đổi dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 thành chuỗi trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6 trong tệp TXT mới, hãy đọc tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và ghi nội dung của nó vào tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 mới bằng cách sử dụng các chức năng
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
9,
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
0,
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
5 và
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
1 mà không cần nhập thư viện.

Để chuyển đổi CSV thành tệp TXT trong Python, hãy thực hiện các bước sau:

  1. Mở tệp CSV ở chế độ đọc và tệp TXT ở chế độ viết.
  2. Đọc tệp CSV và lưu trữ nó trong một biến.
  3. Viết nội dung vào tệp TXT.
  4. Viết nội dung vào tệp TXT.
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:
    content = f_in.read().replace(',', ' ')
    f_out.write(content)

Tại đây, đoạn mã mã giải quyết thách thức cơ bản của chúng tôi:

Thực tế ít được biết đến: Python cho phép nhiều biểu thức trong trình quản lý ngữ cảnh (dòng mở ____22) nếu bạn tách chúng bằng dấu phẩy.

Nội dung của các tệp

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 giống hệt nhau:CSV to a TXT file in Python pandas using the following four steps:

  1. Càng xa càng tốt. Nhưng điều gì sẽ xảy ra nếu bạn có một vấn đề hơi khác nhau:
  2. Phương pháp 2: Phân định không gian trống CSV đến TXT
  3. Thử thách: Làm thế nào để chuyển đổi tệp CSV thành tệp TXT trong Python bằng cách thay thế dấu phân cách
    Name,Job,Age,Income
    Alice,Programmer,23,110000
    Bob,Executive,34,90000
    Carl,Sales,45,50000
    6 bằng khoảng trống
    # 1. Open the CSV file in reading mode and the TXT file in writing mode
    with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:
    
        # 2. Read the CSV file and store in variable
        content = f_in.read()
    
        # 3. Write the content into the TXT file
        f_out.write(content)
    6?
  4. Ví dụ: Chuyển đổi tệp sau ________ 27

Tập tin này

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
8

import pandas as pd

df = pd.read_csv('my_file.csv')
content = str(df)
print(content, file=open('my_file.txt', 'w'))

Ở đây, giải pháp đơn giản cho thử thách này:Little-Known Fact: Python’s

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
2 function allows you to write a string directly into a file object if you use the
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
4 argument as shown in the code snippet.

Nếu bạn muốn thay đổi dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 thành chuỗi trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6 trong tệp TXT mới, hãy đọc tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và ghi nội dung của nó vào tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 mới bằng cách sử dụng các chức năng
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
9,
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
0,
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
5 và
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
1 mà không cần nhập thư viện.

Để chuyển đổi CSV thành tệp TXT trong Python, hãy thực hiện các bước sau:

Đọc tệp CSV thành một chuỗi.

Tạo một chuỗi mới bằng cách thay thế tất cả các lần xuất hiện của dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 bằng chuỗi trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6.

Càng xa càng tốt. Nhưng trong Python, luôn có nhiều cách để giải quyết một vấn đề. Hãy để một cái nhìn về một sự thay thế mạnh mẽ cho phương pháp không có thư viện được sử dụng trước đây:

Phương pháp 3: CSV đến TXT bằng gấu trúc

  1. Càng xa càng tốt. Nhưng điều gì sẽ xảy ra nếu bạn có một vấn đề hơi khác nhau:
  2. Phương pháp 2: Phân định không gian trống CSV đến TXT
  3. Thử thách: Làm thế nào để chuyển đổi tệp CSV thành tệp TXT trong Python bằng cách thay thế dấu phân cách
    Name,Job,Age,Income
    Alice,Programmer,23,110000
    Bob,Executive,34,90000
    Carl,Sales,45,50000
    6 bằng khoảng trống
    # 1. Open the CSV file in reading mode and the TXT file in writing mode
    with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:
    
        # 2. Read the CSV file and store in variable
        content = f_in.read()
    
        # 3. Write the content into the TXT file
        f_out.write(content)
    6?
  4. Ví dụ: Chuyển đổi tệp sau ________ 27
  5. Ví dụ: Chuyển đổi tệp sau ________ 27
import pandas as pd

df = pd.read_csv('my_file.csv')
content = str(df['Name'])
print(content, file=open('my_file.txt', 'w'))

Tập tin này

# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
8

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
0

Ở đây, giải pháp đơn giản cho thử thách này:

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
1

Nếu bạn muốn thay đổi dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 thành chuỗi trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6 trong tệp TXT mới, hãy đọc tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
7 và ghi nội dung của nó vào tệp
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
8 mới bằng cách sử dụng các chức năng
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
9,
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
0,
Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
5 và
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
1 mà không cần nhập thư viện.

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
2

Để chuyển đổi CSV thành tệp TXT trong Python, hãy thực hiện các bước sau:

Đọc tệp CSV thành một chuỗi.

Tạo một chuỗi mới bằng cách thay thế tất cả các lần xuất hiện của dấu phân cách

Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
6 bằng chuỗi trống
# 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out:

    # 2. Read the CSV file and store in variable
    content = f_in.read()

    # 3. Write the content into the TXT file
    f_out.write(content)
6.

Name Job Age Income
Alice Programmer 23 110000
Bob Executive 34 90000
Carl Sales 45 50000
3

Càng xa càng tốt. Nhưng trong Python, luôn có nhiều cách để giải quyết một vấn đề. Hãy để một cái nhìn về một sự thay thế mạnh mẽ cho phương pháp không có thư viện được sử dụng trước đây:

Phương pháp 3: CSV đến TXT bằng gấu trúcLearn More: I have compiled an “ultimate guide” on the Finxter blog that shows you the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.

Văn bản đến CSV: Nếu bạn muốn tìm hiểu cách chuyển đổi tệp văn bản trở lại CSV, vui lòng xem hướng dẫn này trên blog Finxter.: If you want to learn how to convert a text file back to a CSV, feel free to check out this guide on the Finxter blog.

Sự kết luận

Tôi hy vọng bạn thích đọc bài viết này và học được điều gì đó mới. Vui lòng tham gia bản tin email của chúng tôi với các tờ cheat miễn phí và hướng dẫn Python hàng tuần:

Hướng dẫn how do i convert csv data to text in python? - làm cách nào để chuyển đổi dữ liệu csv thành văn bản trong python?

Trong khi làm việc như một nhà nghiên cứu trong các hệ thống phân tán, Tiến sĩ Christian Mayer đã tìm thấy tình yêu của mình đối với việc dạy các sinh viên khoa học máy tính.

Để giúp học sinh đạt được thành công cao hơn của Python, ông đã thành lập trang web giáo dục chương trình Finxter.com. Ông là tác giả của cuốn sách lập trình phổ biến Python Oneer (Nostarch 2020), đồng tác giả của loạt sách Break Break Python, những cuốn sách tự xuất bản, người đam mê khoa học máy tính, freelancer và chủ sở hữu của một trong 10 blog Python lớn nhất trên toàn thế giới.

Niềm đam mê của ông là viết, đọc và mã hóa. Nhưng niềm đam mê lớn nhất của anh là phục vụ các lập trình viên đầy tham vọng thông qua Finxter và giúp họ tăng cường các kỹ năng của họ. Bạn có thể tham gia học viện email miễn phí của anh ấy ở đây.

Làm cách nào để chuyển đổi tệp CSV thành tệp văn bản trong Python?

Các bước để chuyển đổi tệp văn bản thành CSV bằng Python..
Bước 1: Cài đặt gói Pandas. Nếu bạn chưa làm như vậy, hãy cài đặt gói 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 ..

CSV có thể được chuyển đổi thành TXT không?

Các câu hỏi phổ biến về việc chuyển đổi CSV thành TXT Có, McOnverter hỗ trợ chuyển đổi hàng loạt CSV thành TXTS đồng thời. Bạn thậm chí có thể kéo và thả các thư mục chứa CSV để chuyển đổi thành TXT.Yes, MConverter supports batch converting of multiple CSVs to TXTs simultaneously. You can even drag and drop folders containing CSVs to convert to TXT.

Làm cách nào để trích xuất dữ liệu từ tệp CSV trong Python?

Đọc CSV bằng mô -đun sẵn có của Python có tên CSV bằng CSV ...
Nhập thư viện CSV.Nhập CSV ..
Mở tệp CSV.Các .....
Sử dụng đối tượng CSV.Reader để đọc tệp CSV.csvreader = csv.Reader (tệp).
Trích xuất tên trường.Tạo một danh sách trống gọi là tiêu đề.....
Trích xuất các hàng/hồ sơ.....
Đóng tệp ..

Làm cách nào để chuyển đổi CSV thành TXT bằng dấu phẩy?

Xuất dữ liệu vào tệp văn bản bằng cách lưu nó đi vào Tệp> Lưu dưới dạng.Hộp thoại Lưu dưới dạng xuất hiện.Trong hộp lưu dưới dạng, 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).Go to File > Save As. The Save As dialog box appears. In the Save as type box, choose the text file format for the worksheet. For example, click Text (Tab delimited) or CSV (Comma delimited).