Hướng dẫn python file delimiter - dấu phân cách tệp python

2

Nội dung chính ShowShow

  • Làm cách nào để thay đổi dấu phân cách của một tệp?
  • Làm cách nào để thay đổi dấu phân cách trong danh sách Python?
  • Làm thế nào để bạn tìm thấy dấu phân cách của một tệp văn bản trong Python?
  • Làm cách nào để chỉ định một dấu phân cách trong CSV Python?

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.
Learn more.

Tôi chưa quen với Python từ thế giới R và tôi đang làm việc trên các tệp văn bản lớn, được cấu trúc trong các cột dữ liệu (đây là dữ liệu LIDAR, vì vậy thường là 60 triệu + bản ghi).

Có thể thay đổi bộ phân cách trường (ví dụ từ được bỏ qua tab sang phân hủy bằng dấu phẩy) của một tệp lớn như vậy mà không phải đọc tệp và thực hiện vòng lặp

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
6 trên các dòng?

Andre Silva

4.7739 Huy hiệu vàng 50 Huy hiệu bạc64 Huy hiệu Đồng9 gold badges50 silver badges64 bronze badges9 gold badges50 silver badges64 bronze badges

Khi được hỏi ngày 18 tháng 5 năm 2011 lúc 6:28May 18, 2011 at 6:28May 18, 2011 at 6:28

2

Không.

  • Đọc tệp trong
  • Thay đổi bộ phân cách cho mỗi dòng
  • Viết lại từng dòng

Điều này có thể dễ dàng thực hiện chỉ với một vài dòng Python (không được kiểm tra nhưng cách tiếp cận chung hoạt động):

# Python - it's so readable, the code basically just writes itself ;-)
#
with open('infile') as infile:
  with open('outfile', 'w') as outfile:
    for line in infile:
      fields = line.split('\t')
      outfile.write(','.join(fields))

Tôi không quen thuộc với R, nhưng nếu nó có chức năng thư viện cho điều này thì có lẽ nó đang làm chính xác điều tương tự.

Lưu ý rằng mã này chỉ đọc một dòng tại một thời điểm từ tệp, do đó tệp có thể lớn hơn RAM vật lý - nó không bao giờ được tải hoàn toàn.

Đã trả lời ngày 18 tháng 5 năm 2011 lúc 6:33May 18, 2011 at 6:33May 18, 2011 at 6:33

Eli Benderskyeli BenderskyEli BenderskyEli Bendersky

253K87 Huy hiệu vàng344 Huy hiệu bạc406 Huy hiệu đồng87 gold badges344 silver badges406 bronze badges87 gold badges344 silver badges406 bronze badges

4

Bạn có thể sử dụng lệnh Linux tr để thay thế bất kỳ ký tự nào bằng bất kỳ ký tự nào khác.

Đã trả lời ngày 12 tháng 12 năm 2011 lúc 16:17Dec 12, 2011 at 16:17Dec 12, 2011 at 16:17

Trên thực tế, hãy nói có, bạn có thể làm điều đó mà không cần vòng lặp, ví dụ:

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)

Đã trả lời ngày 6 tháng 4 năm 2018 lúc 10:21Apr 6, 2018 at 10:21Apr 6, 2018 at 10:21

5

Bạn không thể, nhưng tôi thực sự khuyên bạn nên kiểm tra máy phát điện.

Điểm là bạn có thể thực hiện chương trình nhanh hơn và có cấu trúc tốt mà không cần phải ghi và lưu trữ dữ liệu trong bộ nhớ để xử lý nó.

Ví dụ

file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)

Mã này dành bộ nhớ để chỉ giữ một dòng.

Đã trả lời ngày 18 tháng 5 năm 2011 lúc 6:56May 18, 2011 at 6:56May 18, 2011 at 6:56

Luka Rahneluka RahneLuka RahneLuka Rahne

10,2K3 Huy hiệu vàng33 Huy hiệu bạc56 Huy hiệu Đồng3 gold badges33 silver badges56 bronze badges3 gold badges33 silver badges56 bronze badges

1

Danh sách các chuỗi và thay thế dấu phân cách, thay thế dấu phân cách hiện tại trong mỗi chuỗi.

Đầu vào: test_list = [Hồi a, t, thì g, f, g, g, w, e , D D O,] Giải thích: Dấu phẩy được thay thế bằng các khoảng trống ở mỗi chuỗi. : test_list = [“a, t”, “g, f, g”, “w, e”, “d, o”], repl_delim = ‘ ‘Output : [“a t”, “g f g”, “w e”, “d o”]Explanation : comma is replaced by empty spaces at each string. : test_list = [“a, t”, “g, f, g”, “w, e”, “d, o”], repl_delim = ‘ ‘
Output : [“a t”, “g f g”, “w e”, “d o”]
Explanation : comma is replaced by empty spaces at each string.

Đầu vào: test_list = [Hồi G#f#g,], repl_delim = ‘, đầu ra: [Hồi g, f, g,] Giải thích: Hash được thay thế bằng dấu phẩy ở mỗi chuỗi. : test_list = [“g#f#g”], repl_delim = ‘, ‘Output : [“g, f, g”]Explanation : hash is replaced by comma at each string. : test_list = [“g#f#g”], repl_delim = ‘, ‘
Output : [“g, f, g”]
Explanation : hash is replaced by comma at each string.

Phương pháp số 1: Sử dụng vòng lặp

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
7 + Sự kết hợp của các hàm trên cung cấp một phương pháp lực lượng vũ phu để giải quyết vấn đề này. Trong đó, một vòng lặp được sử dụng để lặp qua từng chuỗi và thực hiện thay thế bằng cách sử dụng thay thế ().The combination of above functions provide a brute force method to solve this problem. In this, a loop is used to iterate through each string and perform replacement using replace().
The combination of above functions provide a brute force method to solve this problem. In this, a loop is used to iterate through each string and perform replacement using replace().

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
0

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
0
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
2
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1____14
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1____16
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
7
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
0
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
1
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
2
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
3
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
4
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
6
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
7
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
9

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
6

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
91
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
92
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
93
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
94
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
95
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
96
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
97
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
90
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
1
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
2
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
93

Đầu ra:

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9

Phương pháp số 2: Sử dụng danh sách hiểu +

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
7 Kết hợp các chức năng trên có thể cung cấp một lớp lót cho vấn đề này. Điều này tương tự như phương pháp trên, chỉ được đóng gói trong danh sách hiểu.The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension.
The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension.

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
0

with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
0
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
2
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1____14
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
1____16
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
7
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
0
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
1
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
2
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
3
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
4
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
6
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
7
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
file = open("bigfile","w")
j = (i.split("\t") for i in file)
s = (","join(i) for i in j)
#and now magic happens
for i in s:
     some_other_file.write(i)
9

Đầu ra:

Phương pháp số 2: Sử dụng danh sách hiểu +
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
7 Kết hợp các chức năng trên có thể cung cấp một lớp lót cho vấn đề này. Điều này tương tự như phương pháp trên, chỉ được đóng gói trong danh sách hiểu.The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension.

Làm cách nào để thay đổi dấu phân cách của một tệp?

Cách thay đổi bộ phân cách trường (DELIMITER) trong Excel khi lưu dưới dạng tệp CSV...

Trong Microsoft Windows, nhấp vào nút Bắt đầu và sau đó nhấp vào Bảng điều khiển ..

Mở hộp thoại để thay đổi cài đặt khu vực và ngôn ngữ ..

Trong hộp thoại, hãy tìm cài đặt phân tách danh sách. ....

Nhập bộ phân cách danh sách mong muốn ..

Làm cách nào để thay đổi dấu phân cách trong danh sách Python?

Phương thức phân chia chuỗi python () Phương thức phân tách một chuỗi thành một danh sách.Bạn có thể chỉ định phân tách, dấu phân cách mặc định là bất kỳ khoảng trắng nào.Lưu ý: Khi MaxSplit được chỉ định, danh sách sẽ chứa số lượng phần tử được chỉ định cộng với một. The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one. The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Làm thế nào để bạn tìm thấy dấu phân cách của một tệp văn bản trong Python?

Installation...

Cú pháp: Phát hiện (văn bản: str, văn bản: str, default = none, whitelist = [',', ';', ':', '|', '\ t'], blacklist = none).

Văn bản: Chuỗi đầu vào để kiểm tra cho dấu phân cách ..

Mặc định: Giá trị mặc định cho đầu ra trong trường hợp không tìm thấy dấu phân cách hợp lệ nào ..

Làm cách nào để chỉ định một dấu phân cách trong CSV Python?

Làm thế nào để chỉ định một dấu phân cách CSV trong Python...

file_object = open ("delimiter.csv", "w").

Nhà văn = CSV.người viết (file_object, delimiter = ",").

nhà văn.Writerow (["A", "B"]).

File_Object.gần().