Hướng dẫn replace parenthesis in python - thay thế dấu ngoặc đơn trong python

Nếu một đường dẫn có thể chứa dấu ngoặc đơn thì regex r'\[.*?\]' là không đủ:

import os, re

def remove_parenthesized_chunks[path, safeext=True, safedir=True]:
    dirpath, basename = os.path.split[path] if safedir else ['', path]
    name, ext = os.path.splitext[basename] if safeext else [basename, '']
    name = re.sub[r'\[.*?\]', '', name]
    return os.path.join[dirpath, name+ext]

Theo mặc định, hàm bảo tồn các phần ngoặc đơn trong thư mục và phần mở rộng của đường dẫn.

Example:

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'

Trong hướng dẫn này, chúng tôi đã thảo luận về cách loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python. Đối với điều này, phương pháp đơn giản nhất là bằng cách sử dụng hàm replace[] để thay thế mọi lần xuất hiện của dấu ngoặc đơn bằng một chuỗi trống. Chúng tôi cũng thảo luận về việc sử dụng Regex để đạt được điều này. Để sử dụng Regex để loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python, chúng ta có thể sử dụng hàm ____10 hoặc

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
4.

  • Đó là tất cả về cách loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python.
    • Mục lục
    • Các cách để loại bỏ dấu ngoặc ra khỏi chuỗi trong Python
    • Sử dụng hàm thay thế [] để loại bỏ dấu ngoặc đơn khỏi chuỗi trong python
  • A01 hi2 anh

Trong ví dụ trên, dấu ngoặc đơn và mọi thứ trong đó được loại bỏ.

Sự kết luận

Trong hướng dẫn này, chúng tôi đã thảo luận về cách loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python. Đối với điều này, phương pháp đơn giản nhất là bằng cách sử dụng hàm replace[] để thay thế mọi lần xuất hiện của dấu ngoặc đơn bằng một chuỗi trống. Chúng tôi cũng thảo luận về việc sử dụng Regex để đạt được điều này. Để sử dụng Regex để loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python, chúng ta có thể sử dụng hàm ____10 hoặc

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
4.

Đó là tất cả về cách loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python.

Mục lục

Các cách để loại bỏ dấu ngoặc ra khỏi chuỗi trong Python

Ví dụ,

Nhập Pandas ASPD="remove parentheses from [word]"

a2=a1.replace['[',''].replace[']','']  =a1.replace['[',''].replace[']','']  

print[a2]    [a2]    

Output:

Xóa dấu ngoặc đơn ra khỏi Word

Sử dụng chức năng re.Sub[] để loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python

Biểu thức chính quy cung cấp một cách hiệu quả để tìm và thao tác các chuỗi bằng các mẫu regex. Trong Python, chúng tôi sử dụng mô -đun re để làm việc với các biểu thức thông thường.

Chúng ta có thể sử dụng các biểu thức thông thường để loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python. Đối với điều này, chúng tôi sẽ sử dụng hàm

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
0. Hàm này thay thế sự xuất hiện của một chuỗi phù hợp với một số mẫu với một chuỗi khác.

Chúng tôi sẽ tạo một mẫu sẽ phù hợp với các dấu ngoặc đơn này và thay thế chúng bằng một chuỗi trống.

Xem mã bên dưới.

Nhập REre

A1 = "Xóa dấu ngoặc đơn [[từ] [từ]"="remove parentheses [[from] [word]"

a2=re.sub[r'[[]]','',a1]=re.sub[r'[[]]','',a1]

print[a2]    [a2]    

Output:

Xóa dấu ngoặc đơn ra khỏi Word

Sử dụng chức năng
>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
1 để loại bỏ dấu ngoặc đơn khỏi chuỗi trong Python

DataFrame

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
2 có thể lưu trữ dữ liệu theo hàng và cột. Một cột có thể chứa các hàng chuỗi. Do đó, thư viện
>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
2 cũng có nhiều chức năng chuỗi để thao tác và làm việc với dữ liệu đó.

Hàm

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
4 được sử dụng với các đối tượng
>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
5 để thay thế sự xuất hiện của một chuỗi con phù hợp với mẫu biểu thức chính quy [regex] đã cho.

Chúng ta có thể sử dụng chức năng này để loại bỏ dấu ngoặc đơn khỏi các hàng của đối tượng

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
5 cùng một lúc. Chúng ta có thể sử dụng cùng một mẫu chúng tôi đã làm trong ví dụ trước.

Ví dụ,

Nhập Pandas ASPDpandas aspd

df=pd.DataFrame[{'A':['[hello]','hi[]','he[y]']}]=pd.DataFrame[{'A':['[hello]','hi[]','he[y]']}]

df['A']=df['A'].str.replace[r'[[]]',""]['A'] =df['A'].str.replace[r'[[]]',""]

print[df]    [df]    

Output:

A0 hello1 hi2 hey
0 hello
1 hi
2 hey

Chúng ta cũng có thể sử dụng các mẫu Regex khác trong phương pháp này và phương pháp trước đó. Ví dụ: chúng ta có thể sử dụng mẫu

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
7 để loại bỏ dấu ngoặc đơn và bất kỳ chuỗi nào có trong dấu ngoặc đơn.

Xem mã bên dưới.

Nhập Pandas ASPDpandas aspd

df=pd.DataFrame[{'A':['[hello]','hi[]','he[y]']}]=pd.DataFrame[{'A':['[hello]','hi[]','he[y]']}]

A0 hello1 hi2 hey['A'] =df['A'].str.replace[r"\[.*\]",""]

print[df]    [df]    

Output:

Chúng ta cũng có thể sử dụng các mẫu Regex khác trong phương pháp này và phương pháp trước đó. Ví dụ: chúng ta có thể sử dụng mẫu

>>> f = remove_parenthesized_chunks
>>> f["Example_file_[extra_descriptor].ext"]
'Example_file_.ext'
>>> path = r"c:\dir_[important]\example[extra].ext[untouchable]"
>>> f[path]
'c:\\dir_[important]\\example.ext[untouchable]'
>>> f[path, safeext=False]
'c:\\dir_[important]\\example.ext'
>>> f[path, safedir=False]
'c:\\dir_\\example.ext[untouchable]'
>>> f[path, False, False]
'c:\\dir_\\example.ext'
>>> f[r"c:\[extra]\example[extra].ext", safedir=False]
'c:\\\\example.ext'
7 để loại bỏ dấu ngoặc đơn và bất kỳ chuỗi nào có trong dấu ngoặc đơn.
0
1 hi
2 he

Xem mã bên dưới.

df ['a'] = df ['a']. str.replace [r "\ [.*\]", ""]

A01 hi2 anh

Trong ví dụ trên, dấu ngoặc đơn và mọi thứ trong đó được loại bỏ.

Bài Viết Liên Quan

Chủ Đề