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ả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.']
1csv_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.']
2csv_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.']
4csv_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.']
5csv_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.']
6csv_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.']
7csv_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.']
9Traceback [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
0Traceback [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
1Traceback [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
2Traceback [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
3csv_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
5Traceback [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
6Traceback [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
7Traceback [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
8Traceback [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
9Traceback [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
3Traceback [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
4Traceback [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
5csv_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.']
9Traceback [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
0Traceback [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
9Traceback [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
2Traceback [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
0csv_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 2003
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
6Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2005
Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2006
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
7Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2008
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
3FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2000
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 2001
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 2003
Apple Banana JackFruit Orange Pineapple Guava Grapes Mango 100 70 30 120 90 50 80 2006
FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2005
FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2006
FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2007
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.']
6FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2001
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
5import 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 2001
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 pandas0
FRUIT_NAME PRICE Apple 100 Banana 70 JackFruit 30 Orange 120 Pineapple 90 Guava 50 Grapes 80 Mango 2006__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
3pip install pandas5
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.