Từ một tệp, tôi đã lấy một dòng, chia dòng thành 5 cột bằng
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
1. Nhưng tôi phải viết các cột đó dưới dạng các giá trị phân tách tab trong một tệp đầu ra.Hãy nói rằng tôi có ____ 12 ... Tổng cộng có 5 mục. Làm thế nào tôi có thể đạt được điều này bằng cách sử dụng Python? Ngoài ra, tôi không thể ghi các giá trị
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
2 vào tệp đầu ra.Tôi đã thử cả hai mã này, cả hai đều không hoạt động [tôi đang sử dụng Python 2.6]:
Mã 1:
with open['output', 'w']:
print l[1], l[2], l[3], l[4], l[5] > output
Mã 2:
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
Viết các giá trị được phân tách tab vào một tệp trong Python #
Để ghi các giá trị phân tách tab vào một tệp:
- Mở tệp ở chế độ viết.
- Sử dụng ký tự
4 để chèn một tab vào một chuỗi.with open['output', 'w'] as outf: outf.write[l[1], l[2], l[3], l[4], l[5]]
- Sử dụng phương thức
5 để ghi chuỗi vào tệp.with open['output', 'w'] as outf: outf.write[l[1], l[2], l[3], l[4], l[5]]
Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Chúng tôi đã sử dụng câu lệnh
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
6 để mở tệp ở chế độ viết.Câu lệnh
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
6 tự động chăm sóc việc đóng tệp cho chúng tôi.Nếu bạn chỉ cần chèn một tab vào một chuỗi, hãy sử dụng ký tự
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
4.Copied!
with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond']
Nội dung của tệp
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
9 trông như thế này.Copied!
bobby hadz first second
Bạn có thể đọc tệp để xác minh nó chứa các tab.
Copied!
with open['example.txt', 'r', encoding='utf-8'] as f: lines = f.readlines[] print[lines] # 👉️ ['bobby\thadz\n', 'first\tsecond']
Bạn có thể sử dụng ký tự
0 nếu bạn cần thêm một dòng mới sau chuỗi.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Nếu bạn cần tách các mục của danh sách bằng các tab và ghi kết quả vào tệp, hãy sử dụng phương thức Copied!
1.# ✅ write strings separated by tabs to file
with open['example.txt', 'w', encoding='utf-8'] as my_file:
my_file.write['bobby' + '\t' + 'hadz' + '\n']
my_file.write['first\tsecond']
# ✅ separate the items of a list by tabs and write the result to a file
with open['example.txt', 'w', encoding='utf-8'] as my_file:
my_list = ['bobby', 'hadz', 'com']
my_file.write['\t'.join[my_list] + '\n']
Copied!
with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.
Lưu ý rằng phương pháp này sẽ tăng
2 nếu có bất kỳ giá trị không chuỗi nào trong ITEBLEBLE.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Nếu bạn có thể xác định được số hoặc các loại khác, hãy chuyển đổi tất cả các giá trị thành chuỗi trước khi gọi
3.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Copied!
with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 1, 'hadz', 2, 'com'] my_file.write['\t'.join[str[item] for item in my_list] + '\n']
Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.
Chúng tôi đã gọi phương thức
1 trên một chuỗi chứa một tab để tham gia các phần tử trong danh sách với một dấu phân cách tab.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Nội dung của tập tin trông như thế này.
Copied!
bobby 1 hadz 2 com
Ngoài ra, bạn có thể truy cập từng mục danh sách tại một chỉ mục và sử dụng toán tử bổ sung [+].
Copied!
with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 1, 'hadz', 2, 'com'] my_file.write[ my_list[0] + '\t' + str[my_list[1]] + '\t' + my_list[2] + '\t' + str[my_list[3]] + '\t' + my_list[4] + '\n' ]
Toán tử bổ sung [+] có thể được sử dụng để nối các chuỗi.
Tuy nhiên, bạn phải đảm bảo rằng các giá trị ở bên trái và bên phải của toán tử là loại chuỗi.
Nếu bạn có các giá trị thuộc loại khác, hãy sử dụng lớp
5 để chuyển đổi chúng thành chuỗi.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Bạn cũng có thể sử dụng vòng lặp
6 để lặp qua danh sách và thêm một tab sau mỗi mục.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
with open['output', 'w'] as outf:
outf.write[l[1], l[2], l[3], l[4], l[5]]
0Vòng lặp
6 lặp lại trong danh sách và sử dụng toán tử bổ sung [+] để thêm một tab sau mỗi mục.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']
Bước cuối cùng là viết một ký tự dòng mới [
0] vào tệp.Copied!
# ✅ write strings separated by tabs to file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_file.write['bobby' + '\t' + 'hadz' + '\n'] my_file.write['first\tsecond'] # ✅ separate the items of a list by tabs and write the result to a file with open['example.txt', 'w', encoding='utf-8'] as my_file: my_list = ['bobby', 'hadz', 'com'] my_file.write['\t'.join[my_list] + '\n']