Hướng dẫn how do you change the delimiter in a text file in python? - làm cách nào để thay đổi dấu phân cách trong tệp văn bản trong python?

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 for trên các dòng?

Hướng dẫn how do you change the delimiter in a text file in python? - làm cách nào để thay đổi dấu phân cách trong tệp văn bản trong python?

Andre Silva

4.7839 Huy hiệu vàng51 Huy hiệu bạc65 Huy hiệu Đồng9 gold badges51 silver badges65 bronze badges

Khi được hỏi ngày 18 tháng 5 năm 2011 lúc 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:33

Eli Benderskyeli BenderskyEli Bendersky

255K87 Huy hiệu vàng344 Huy hiệu bạc408 Huy hiệu đồng87 gold badges344 silver badges408 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: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: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:56

Luka Rahneluka RahneLuka Rahne

10,2K3 Huy hiệu vàng33 Huy hiệu bạc56 Huy hiệu Đồng3 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.

Đầ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.

Phương pháp số 1: Sử dụng vòng lặp replace() + 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().

test_list = [

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=
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=
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

for

The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
1
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
2
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
3

The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
4
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
5
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
6
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
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
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
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
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
3

Đầu ra:

The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']

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.
The combination of above functions can provide one liner to this problem. This is similar to above method, just encapsulated in list comprehension.

test_list = [

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=
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=
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)
8
with open('in') as infile:
  with open('out', 'w') as outfile:
      map(lambda line: outfile.write(','.join(line.split('\n'))), infile)
9
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
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
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
3

Đầu ra:

The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']