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?
- Thử thách cơ bản
- Phương pháp 1: CSV đến TXT không thay đổi
- Phương pháp 2: Phân định không gian trống CSV đến TXT
- Phương pháp 3: CSV đến TXT bằng gấu trúc
- Phương pháp 4: Các cột hoặc hàng CSV đến TXT bằng cách sử dụng gấu trúc
- Lập trình viên hài hước
- Thêm chuyển đổi Python CSV
- Sự kết luận
Thử thách cơ bản
Phương pháp 1: CSV đến TXT không thay đổiChallenge: How to convert a CSV file to a text file in Python?
Phương pháp 2: Phân định không gian trống CSV đến TXT
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000
Phương pháp 3: CSV đến TXT bằng gấu trúc
Nghề nghiệp | Tuổi tác | 23 | 110000 |
Thu nhập = earnings | Alice | 34 | 90000 |
Lập trình viên | Bob | 45 | 50000 |
Chấp hành, quản lý
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000
Carl
Việc bán hàngHow 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
Phương pháp 2: Phân định không gian trống CSV đến TXT
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:
- Mở tệp CSV ở chế độ đọc và tệp TXT ở chế độ viết.
- Đọc tệp CSV và lưu trữ nó trong một biến.
- 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ở
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] 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 [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 opening line] if you separate them with a comma. Nội dung của các tệp
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 và 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 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ế DELIMITER
0 bằng khoảng trống 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?: How to convert a CSV file to a TXT file in Python by replacing the delimiter 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
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 with the empty space 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?Ví dụ: Chuyển đổi tệp sau ________ 31: Convert the following file
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…Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000
Tập tin này
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
2Name 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 DELIMITER
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 thành chuỗi trống 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 trong tệp TXT mới, hãy đọc tệp 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 và ghi nội dung của nó vào tệp 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 mới bằng cách sử dụng các chức năng 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, 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
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
9 và 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 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:
- Mở tệp CSV ở chế độ đọc và tệp TXT ở chế độ viết.
- Đọ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
0 bằng chuỗi trốngTraceback [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
- Viết nội dung vào tệp TXT.
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.']
0Tạ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ở 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] nếu bạn tách chúng bằng dấu phẩ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
Nội dung của các tệp
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 và 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 giống hệt nhau:CSV to a TXT file in Python pandas using the following four steps:- 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ế DELIMITER
0 bằng khoảng trốngTraceback [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
- Ví dụ: Chuyển đổi tệp sau ________ 31
Tập tin này
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
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.']
1Ở đâ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,500006 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,500008 argument as shown in the code snippet.
Nếu bạn muốn thay đổi DELIMITER
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 thành chuỗi trống 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 trong tệp TXT mới, hãy đọc tệp 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 và ghi nội dung của nó vào tệp 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 mới bằng cách sử dụng các chức năng 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, 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
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
9 và 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 mà không cần nhập thư viện.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Để 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 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 bằng chuỗi trống 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.
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
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
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:
- 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ế DELIMITER
0 bằng khoảng trốngTraceback [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
- Ví dụ: Chuyển đổi tệp sau ________ 31
- Ví dụ: Chuyển đổi tệp sau ________ 31
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
2Ở đây, giải pháp đơn giản cho thử thá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.']
4Nếu bạn muốn thay đổi DELIMITER
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 thành chuỗi trống 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 trong tệp TXT mới, hãy đọc tệp 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 và ghi nội dung của nó vào tệp 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 mới bằng cách sử dụng các chức năng 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, 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
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
9 và 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 mà không cần nhập thư viện.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Để chuyển đổi CSV thành tệp TXT trong Python, hãy thực hiện các bước sau:
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Đọ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 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 bằng chuỗi trống 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.
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
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
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:
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,500007
Phương pháp 3: CSV đến TXT bằng gấu trúc
Giả sử bạn đã cài đặt gấu trúc trong môi trường địa phương của mình, bạn có thể viết CSV vào tệp TXT trong Python Pandas bằng bốn bước sau:Learn 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.
Nhập thư viện Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
3.: 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.
Đọc tệp CSV vào DataFrame bằng Name,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000
4.
Chuyển đổi DataFrame thành một chuỗi bằng hàm
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,500005 tích hợp.
Ví dụ, in chuỗi vào tệp bằng đối số tệp của hàm
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,500006 chẳng hạn.
Ở đây, ví dụ về Python cơ bản:
Thực tế ít được biết đến: Chức năng Python từ
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,500006 cho phép bạn ghi một chuỗi trực tiếp vào đối tượng tệp nếu bạn sử dụng đối số
Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,500008 như trong đoạn mã.