Hướng dẫn python remove first and last line from string - python xóa dòng đầu tiên và dòng cuối cùng khỏi chuỗi

Tùy thuộc vào cách mà trường hợp sử dụng của bạn sẽ tiêu thụ chuỗi, cách loại bỏ nhanh hơn có thể bằng cách không loại bỏ nó.

Nếu bạn có kế hoạch truy cập các dòng trong chuỗi tuần tự, bạn có thể xây dựng một trình tạo bỏ qua dòng đầu tiên và cuối cùng trong khi mang lại cho mỗi dòng khi được tiêu thụ thay vì xây dựng một bộ bản sao mới của tất cả các dòng hoàn toàn.

Một cách ad-hoc để tránh dòng đầu tiên và cuối cùng là lặp lại chuỗi mà không tạo ra các bản sao không cần thiết là bằng cách theo dõi ba dòng tiếp theo và chỉ trả lại cái thứ 2, theo cách này, việc lặp lại sẽ kết thúc trước khi đạt đến dòng cuối cùng mà không yêu cầu Để biết vị trí của lần phá vỡ dòng cuối cùng.

Chức năng sau sẽ cung cấp cho bạn đầu ra mong muốn:

def split_generator[s]:
  # Keep track of start/end positions for three lines
  start_prev = end_prev = 0
  start = end = 0
  start_next = end_next = 0

  nr_lines = 0

  for idx, c in enumerate[s]:
    if c == '\n':
      nr_lines += 1

      start_prev = start
      end_prev = end
      start = start_next
      end = end_next
      start_next = end_next
      end_next = idx

      if nr_lines >= 3:
        yield s[[start + 1] : end]

  # Handle the case when input string does not finish on "\n"
  if s[-1] != '\n' and nr_lines >= 2:
    yield s[[start_next+1]:end_next]

Bạn không thể kiểm tra nó với:

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]

Sẽ tạo ra đầu ra:

1st example
second
2nd example
second
3rd example
second
third
4th example
second
third
5th example
second
third
fourth

Lưu ý rằng lợi thế lớn nhất của phương pháp này là sẽ chỉ tạo một dòng mới vào thời điểm đó và sẽ hầu như không mất thời gian để tạo ra dòng đầu ra đầu tiên [thay vì chờ tất cả các dòng được tìm thấy trước khi tiến hành tiếp tục], nhưng, một lần nữa , điều đó có thể hữu ích hoặc không tùy thuộc vào trường hợp sử dụng của bạn.

Xóa các dòng đầu tiên và cuối cùng khỏi một chuỗi trong python #

Để xóa các dòng đầu tiên và cuối cùng khỏi một chuỗi:

  1. Sử dụng phương thức str.find[] để lấy chỉ mục của ký tự dòng đầu tiên.
  2. Sử dụng phương thức str.rfind[] để lấy chỉ mục của ký tự dòng cuối cùng.
  3. Nhận một lát của chuỗi từ dòng thứ hai cho đến dòng cuối cùng.

Copied!

my_str = """\ first line second line third line fourth line""" result = my_str[my_str.find['\n'] + 1: my_str.rfind['\n']] # second line # third line print[result]

Chúng tôi đã sử dụng phương thức str.find[] để lấy chỉ mục của ký tự dòng đầu tiên trong chuỗi.

Copied!

my_str = """\ first line second line third line fourth line""" print[my_str.find['\n']] # 👉️ 10

Phương thức str.find trả về chỉ mục của lần xuất hiện đầu tiên của chuỗi con được cung cấp trong chuỗi.

Phương thức trả về

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
1 nếu không tìm thấy chuỗi con trong chuỗi.

Lưu ý rằng chúng tôi đã thêm

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
2 vào kết quả vì chúng tôi muốn loại trừ ký tự mới.

Copied!

my_str = """\ first line second line third line fourth line""" result = my_str[my_str.find['\n'] + 1: my_str.rfind['\n']] # second line # third line print[result]

Chúng tôi đã sử dụng phương thức str.rfind[] để lấy chỉ mục của ký tự dòng cuối cùng trong chuỗi.

Copied!

my_str = """\ first line second line third line fourth line""" print[my_str.rfind['\n']] # 👉️ 33

Phương thức str.rfind trả về chỉ số cao nhất trong chuỗi nơi tìm thấy chuỗi con được cung cấp.

Phương thức trả về

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
1 nếu không chứa chuỗi con trong chuỗi.

Bước cuối cùng là lấy một lát chuỗi từ dòng thứ hai lên đến, nhưng không bao gồm dòng cuối cùng.

Copied!

my_str = """\ first line second line third line fourth line""" result = my_str[my_str.find['\n'] + 1: my_str.rfind['\n']] # second line # third line print[result]

Cú pháp để cắt chuỗi là

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
5, trong đó chỉ số
print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
6 bao gồm và chỉ số
print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
7 là độc quyền [lên đến, nhưng không bao gồm].

Chỉ số

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
6 bao gồm, vì vậy chúng tôi đã thêm
print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
2 để bắt đầu sau ký tự dòng mới.

Chỉ mục

print["1st example"]
for filtered_strs in split_generator['first\nsecond\nthird']:
  print[filtered_strs]

print["2nd example"]
for filtered_strs in split_generator['first\nsecond\nthird\n']:
  print[filtered_strs]

print["3rd example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth']:
  print[filtered_strs]

print["4th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\n']:
  print[filtered_strs]

print["5th example"]
for filtered_strs in split_generator['first\nsecond\nthird\nfourth\nfifth']:
  print[filtered_strs]
7 là độc quyền để chúng tôi có thể chỉ định trực tiếp nó để lấy một lát chuỗi từ dòng thứ hai cho đến dòng cuối cùng.

Bài Viết Liên Quan

Chủ Đề