Hướng dẫn how do you change the delimiter in python? - làm cách nào để thay đổi dấu phân cách trong python?

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 =

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
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
8
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
9
# 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))
0
# 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))
1
# 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))
2

# 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))
3=
# 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))
5

# 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))
6=
# 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))
8

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

Đầ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']

Phương pháp số 2: Sử dụng danh sách hiểu + replace() Sự kết hợp của các hàm 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.

test_list =

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
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
8
The original list is : ['a, t', 'g, f, g', 'w, e', 'd, o']
Replaced List : ['a#t', 'g#f#g', 'w#e', 'd#o']
9
# 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))
0
# 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))
1
# 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))
2

# 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))
3=
# 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))
5

# 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))
6=
# 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))
8

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

Đầ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']


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

# 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))
9 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 badges

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

Hướng dẫn how do you change the delimiter in python? - làm cách nào để thay đổi dấu phân cách trong python?

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

253K87 Huy hiệu vàng344 Huy hiệu bạc406 Huy hiệu đồng87 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: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

Làm thế nào để tôi thay đổi dấu phân cách của tôi?

Để buộc nó sử dụng một dấu phân cách khác, hãy tiến hành các bước sau: Nhấp vào Tệp> Tùy chọn> Tùy chọn chỉnh sửa Advanced.Click File > Options > Advanced. Under Editing options, clear the Use system separators check box. Change the default Decimal separator.

Làm cách nào để thay đổi dấu phân cách trong tệp văn bản?

Thay đổi dấu phân cách được sử dụng khi nhập tệp văn bản Nếu bạn sử dụng get & chuyển đổi dữ liệu> từ văn bản/csv, sau khi bạn chọn tệp văn bản và nhấp vào nhập, chọn một ký tự để sử dụng từ danh sách theo Delimiter.If you use Get & Transform Data > From Text/CSV, after you choose the text file and click Import, choose a character to use from the list under Delimiter.

Làm thế nào để bạn thêm Delimit trong Python?

Phương pháp số 1: Sử dụng loop + str () Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện.Trong đó, chúng tôi chạy một vòng lặp để thêm dấu phân cách ở cuối mỗi phần tử, sau khi chuyển đổi từng phần tử thành chuỗi.Using loop + str() This is one of the ways in which this task can be performed. In this, we run a loop to add delimiter at end of each element, after converting each element to string.

Delimiter ở đâu 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 ..