Hướng dẫn how do i convert a csv file to a text file in python? - làm cách nào để chuyển đổi tệp csv sang tệp văn bản trong python?

Tôi muốn chuyển đổi một vài tệp .csv thành các tệp .txt bằng Python. Trong các tệp .csv của tôi, tôi có hàng trăm dòng dữ liệu như The Bellow: Hình ảnh của tệp CSV

Value   Date    Time
919     4/15/2016   19:41:02
551     4/15/2016   19:46:51
717     4/15/2016   19:49:48
2679    4/15/2016   19:52:49
2890    4/15/2016   19:55:43
2897    4/15/2016   19:58:38
1790    4/15/2016   21:39:14
2953    4/15/2016   21:42:10
2516    4/15/2016   21:45:04
2530    4/15/2016   21:47:58
2951    4/15/2016   21:51:02
2954    4/15/2016   21:53:56
2537    4/15/2016   21:56:52
2523    4/15/2016   21:59:45
2536    4/15/2016   22:02:49
2727    4/15/2016   22:05:43

Tôi sử dụng mã dưới cho mục đích này.

csv_file = input('Enter the name of your input file: ')
txt_file = input('Enter the name of your output file: ')

text_list = []

with open(csv_file, "r") as my_input_file:
    for line in my_input_file:
        line = line.split(",", 2)
        text_list.append(" ".join(line))

with open(txt_file, "w") as my_output_file:
    my_output_file.write("#1\n")
    my_output_file.write("double({},{})\n".format(len(text_list), 2))
    for line in text_list:
        my_output_file.write("  " + line)
    print('File Successfully written.')

Vấn đề đầu tiên của tôi là khi tên của tệp đầu vào là (ví dụ) "DFW002_0330PM_Thursday_November_16_2017", tôi nhận được lỗi dưới đây:

Traceback (most recent call last):
  File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
    csv_file = input('Enter the name of your input file: ')
  File "", line 1, in 
NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined

Nhưng, khi tôi thay đổi tên của mã thành (ví dụ) "11", mã xác định tệp và chuyển sang các bước tiếp theo, nhưng một lần nữa nó trả về lỗi dưới:

Traceback (most recent call last):
  File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
    with open(csv_file, "r") as my_input_file:
TypeError: coercing to Unicode: need string or buffer, int found

Bạn vui lòng giúp tôi xử lý những vấn đề này?

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Approach:

    • Đọc tệp .csv bằng cách sử dụng gấu trúc DataFrame.
    • Chuyển đổi cột cụ thể thành danh sách bằng hàm tạo Danh sách ()
    • Sau đó, tuần tự chuyển đổi từng phần tử của danh sách thành một chuỗi và tham gia chúng bằng một ký tự hoặc không gian cụ thể.

    Đối với chương trình của chúng tôi, chúng tôi sẽ sử dụng tệp CSV sau:

    Code:

    Python3

    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    0
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    1

    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    2
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    4
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    5
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    6

    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    7
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    9
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    0
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    1
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    2

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    3
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    5
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    6
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    7
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    8
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    9

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    3
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    4

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    5
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    9
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    0
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    9
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    2

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    0
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200
    3
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    6
    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200
    5
    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200
    6
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    7
    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200
    8

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    3
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    0

    Output:

    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200

    Làm thế nào để chuyển đổi tất cả các cột CSV thành văn bản?

    Đối với điều này, chúng tôi không cần nhập bất kỳ thư viện nào.

    Code:

    Python3

    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    1
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    3
    Apple Banana JackFruit Orange Pineapple Guava Grapes Mango
    
    100
    70
    30
    120
    90
    50
    80
    200
    6
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    5
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    6
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    7
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    6

    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    1
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    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)
    
    2
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 1, in 
        csv_file = input('Enter the name of your input file: ')
      File "", line 1, in 
    NameError: name 'DFW000_0330PM_Thursday_November_16_2017' is not defined
    
    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)
    
    4
    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    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)
    
    6

    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    1
    csv_file = input('Enter the name of your input file: ')
    txt_file = input('Enter the name of your output file: ')
    
    text_list = []
    
    with open(csv_file, "r") as my_input_file:
        for line in my_input_file:
            line = line.split(",", 2)
            text_list.append(" ".join(line))
    
    with open(txt_file, "w") as my_output_file:
        my_output_file.write("#1\n")
        my_output_file.write("double({},{})\n".format(len(text_list), 2))
        for line in text_list:
            my_output_file.write("  " + line)
        print('File Successfully written.')
    
    3
    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
    pip install pandas
    
    0
    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200
    6__72

    Traceback (most recent call last):
      File "C:/Users/Behzad/Desktop/run/UTA/cvstotext.py", line 6, in 
        with open(csv_file, "r") as my_input_file:
    TypeError: coercing to Unicode: need string or buffer, int found
    
    3
    pip install pandas
    
    5

    Output:

    FRUIT_NAME PRICE
    Apple 100
    Banana 70
    JackFruit 30
    Orange 120
    Pineapple 90
    Guava 50
    Grapes 80
    Mango 200

    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:

    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)
    

    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.

    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ói Pandas trong Windows:

    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

    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ước 3: Chỉ định đường dẫn nơi tệp CSV mới sẽ được lưu

    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

    Bước 4: Chuyển đổi tệp văn bản thành CSV bằng Python

    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:

    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)
    

    Ví dụ của chúng tôi:

    • Đường dẫn lưu trữ tệp văn bản là: C: \ Users \ ron \ Desktop \ test \ Product_list.txtC:\Users\Ron\Desktop\Test\Product_List.txt
      • Trong đó tên tệp là sản phẩm_list & nbsp; và phần mở rộng tệp là txtProduct_List and the file extension is txt
    • Đường dẫn mà CSV sẽ được lưu là: C: \ Users \ Ron \ Desktop \ test \ new_products.csvC:\Users\Ron\Desktop\Test\New_Products.csv
      • Trong đó tên tệp mới được tạo là new_products và phần mở rộng tệp là CSVNew_Products and the file extension is csv

    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):

    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)
    

    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.

    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 để mở tệp CSV trong tệp văn bản?

    Trả lời: Bạn có thể mở tệp CSV trên Google Sheet, Notepad hoặc OpenOffice Calc.Chỉ cần nhấp chuột phải vào tệp, chọn mở và chọn OpenOffice calc hoặc Notepad.Để mở trong Google Sheets, hãy truy cập tùy chọn tệp trong Google Sheet, nhấp vào Nhập, chọn tệp CSV bạn muốn mở, nhấp vào Nhập.right-click on the file, select Open With and pick either OpenOffice Calc or Notepad. To open in Google Sheets, go to the File option in Google Sheet, click import, select the CSV file you want to open, click import.

    Làm thế nào để bạn tạo một tệp TXT trong Python?

    Trong Python, bạn sử dụng hàm Open () với một trong các tùy chọn sau - "X" hoặc "W" - để tạo một tệp mới: "X" - Tạo: Lệnh này sẽ tạo một tệp mới khi và chỉ khi cóKhông có tệp đã tồn tại với tên đó nếu không nó sẽ trả về lỗi.use the open() function with one of the following options – "x" or "w" – to create a new file: "x" – Create: this command will create a new file if and only if there is no file already in existence with that name or else it will return an error.

    Làm cách nào để chuyển đổi tệp CSV thành 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 ..