Hướng dẫn replace backslash python - thay thế Python bằng dấu gạch chéo ngược

6

Nội dung chính ShowShow

  • Sử dụng thay thế ()
  • Sử dụng hàm dịch ()
  • Sử dụng biểu thức chính quy (re.sub ())
  • Phương pháp 1: Sử dụng hàm thay thế chuỗi sẵn ()
  • Phương pháp 2: Sử dụng hàm dịch () trong Python
  • Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Python
  • Làm thế nào để bạn thay đổi chém về phía trước bằng dấu gạch chéo ngược trong Python?
  • Làm thế nào để tôi thoát khỏi sự chém về phía trước trong Python?
  • Python có sử dụng dấu gạch chéo hay ngược không?
  • Làm cách nào để thay đổi dấu gạch chéo ngược trong 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 có

foo = '/DIR/abc'

và tôi muốn chuyển đổi nó thành

bar = '\\MYDIR\data\abc'

Vì vậy, đây là những gì tôi làm trong Python:

>>> foo = '/DIR/abc'
>>> bar = foo.replace(r'/DIR/',r'\\MYDIR\data\')
  File "", line 1
    bar = foo.replace(r'/DIR/',r'\\MYDIR\data\')
                                                 ^
SyntaxError: EOL while scanning string literal

Tuy nhiên, nếu tôi cố gắng thoát khỏi dấu gạch chéo ngược cuối cùng bằng cách nhập thay vào đó

bar = '\\MYDIR\data\abc'
3, thì tôi sẽ nhận được sự quái dị này:

>>> bar2
'\\\\MYDIR\\data\\\\abc'

Cứu giúp! Điều này đang khiến tôi phát điên.

Đã hỏi ngày 31 tháng 1 năm 2013 lúc 21:12Jan 31, 2013 at 21:12Jan 31, 2013 at 21:12

Hướng dẫn replace backslash python - thay thế Python bằng dấu gạch chéo ngược

1

Đối số thứ hai phải là một chuỗi, không phải là một mẫu regex:

foo.replace(r'/DIR/', '\\\\MYDIR\\data\\')

Đã trả lời ngày 31 tháng 1 năm 2013 lúc 21:16Jan 31, 2013 at 21:16Jan 31, 2013 at 21:16

Thuốc nổ PillsexplosionExplosion PillsExplosion Pills

185K49 Huy hiệu vàng318 Huy hiệu bạc395 Huy hiệu Đồng49 gold badges318 silver badges395 bronze badges49 gold badges318 silver badges395 bronze badges

11

Lý do bạn gặp phải điều này là do hành vi của cú pháp

bar = '\\MYDIR\data\abc'
4, lấy một số lời giải thích từ tài liệu Python

r "\" "là một chuỗi hợp lệ theo nghĩa đen bao gồm hai ký tự: dấu gạch chéo ngược và báo giá kép; r" \ "không phải là một chuỗi hợp lệ theo nghĩa đen (ngay cả một chuỗi thô không thể kết thúc trong một số lượng ngược của dấu gạch chéo ngược). Cụ thể, một Chuỗi RAW không thể kết thúc trong một dấu gạch chéo ngược duy nhất (vì dấu gạch chéo ngược sẽ thoát khỏi ký tự trích dẫn sau).

Vì vậy, bạn sẽ cần sử dụng một chuỗi thoát ra bình thường cho đối số cuối cùng.

>>> foo = "/DIR/abc"
>>> print foo.replace(r"/DIR/", "\\\\MYDIR\\data\\")
\\MYDIR\data\abc

Đã trả lời ngày 31 tháng 1 năm 2013 lúc 21:22Jan 31, 2013 at 21:22Jan 31, 2013 at 21:22

SerdalisserdalisSerdalisSerdalis

9,9972 Huy hiệu vàng37 Huy hiệu bạc57 Huy hiệu Đồng2 gold badges37 silver badges57 bronze badges2 gold badges37 silver badges57 bronze badges

Tôi chỉ cần đặt một

bar = '\\MYDIR\data\abc'
5 trước

bar = '\\MYDIR\data\abc'
0 để thay đổi chém về phía trước.
inv_num = line.replace(r'/', '-')

Ramesh-X

4.3856 Huy hiệu vàng46 Huy hiệu bạc60 Huy hiệu Đồng6 gold badges46 silver badges60 bronze badges6 gold badges46 silver badges60 bronze badges

Đã trả lời ngày 13 tháng 6 năm 2017 lúc 14:55Jun 13, 2017 at 14:55Jun 13, 2017 at 14:55

Hai vấn đề:

  1. Một chữ thô đơn giản là không thể kết thúc bằng một dấu gạch chéo ngược duy nhất vì nó được hiểu là thoát khỏi ký tự trích dẫn. Do đó, sử dụng một nghĩa đen (không raw) thông thường với lối thoát:
    bar = '\\MYDIR\data\abc'
    
    1.
  2. Khi được hiển thị (sử dụng kiểu
    bar = '\\MYDIR\data\abc'
    
    2), các chuỗi sẽ xuất hiện với lối thoát. Do đó,
    bar = '\\MYDIR\data\abc'
    
    3 chỉ có hai dấu gạch chéo ngược thực tế. Vì vậy,
    bar = '\\MYDIR\data\abc'
    
    4 thực sự là
    bar = '\\MYDIR\data\abc'
    
    5.

Đã trả lời ngày 31 tháng 1 năm 2013 lúc 21:23Jan 31, 2013 at 21:23Jan 31, 2013 at 21:23

Nneonneonneonneonneonneonneonneo

166K35 Huy hiệu vàng297 Huy hiệu bạc368 Huy hiệu Đồng35 gold badges297 silver badges368 bronze badges35 gold badges297 silver badges368 bronze badges

path = path.replace (r "/", "\") sẽ thay thế đường dẫn = c:/thư mục bằng đường dẫn = c: \ thư mục

Đã trả lời ngày 27 tháng 4 lúc 6:40Apr 27 at 6:40Apr 27 at 6:40

1

  • Sử dụng thay thế ()
  • Sử dụng hàm dịch ()
  • Sử dụng biểu thức chính quy (re.sub ())
  • Backslash trong Python
  • Phương pháp 1: Sử dụng hàm thay thế chuỗi sẵn ()
  • Phương pháp 2: Sử dụng hàm dịch () trong Python
  • Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Python

Làm thế nào để bạn thay đổi chém về phía trước bằng dấu gạch chéo ngược trong Python?String replace() function, translate() method, or regular expression(re.sub()).

Làm thế nào để tôi thoát khỏi sự chém về phía trước trong Python?

  • Python có sử dụng dấu gạch chéo hay ngược không? Using replace()
  • Làm cách nào để thay đổi dấu gạch chéo ngược trong Python? Using translate() function
  • 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. Using regular expression (re.sub())
  • tôi có Backslash in Python
  • và tôi muốn chuyển đổi nó thành Method 1: Using the inbuilt String replace() function
  • Vì vậy, đây là những gì tôi làm trong Python: Method 2: Using translate() function in Python
  • Tuy nhiên, nếu tôi cố gắng thoát khỏi dấu gạch chéo ngược cuối cùng bằng cách nhập thay vào đó
    bar = '\\MYDIR\data\abc'
    
    3, thì tôi sẽ nhận được sự quái dị này:
    Method 3: Using regular expression (re.sub()) in Python

Sử dụng thay thế ()

example_string="path/to/check"="path/to/check"="path/to/check"

Cứu giúp! Điều này đang khiến tôi phát điên.=example_string.replace("/","\\")

print(new_string)(new_string)(new_string)

Output:

Sử dụng hàm dịch ()

stringn="book/pencil/ink/pen/rubber"="book/pencil/ink/pen/rubber"="book/pencil/ink/pen/rubber"

Đã hỏi ngày 31 tháng 1 năm 2013 lúc 21:12Jan 31, 2013 at 21:12=stringn.translate(str.maketrans({'/':'\\'}))

print(stringn1)(stringn1)(stringn1)

Output:

Đối số thứ hai phải là một chuỗi, không phải là một mẫu regex:\pencil\ink\pen\rubber

Sử dụng biểu thức chính quy (re.sub ())

Backslash trong Pythonrere

string1="path/to/check/and/edit"="path/to/check/and/edit"="path/to/check/and/edit"

Phương pháp 1: Sử dụng hàm thay thế chuỗi sẵn ()=re.sub("/",r"\\",string1)=re.sub("/",r"\\",string1)

print(string2)(string2)(string2)

Output:

Phương pháp 2: Sử dụng hàm dịch () trong Python

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonis used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.is used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.

Một slash (/) trong chuỗi python có thể được thay thế bằng một dấu gạch chéo ngược (\) bằng cách sử dụng hàm former setper (), phương thức dịch () hoặc biểu thức chính quy (re.sub ()).used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.

Nội dung

Output:

1 sử dụng thay thế ()example”string because the second quotation marks have been rendered literal string character by the escape character -the backslash.example”string because the second quotation marks have been rendered literal string character by the escape character -the backslash.

2 sử dụng hàm dịch ()

Phương pháp 1: Sử dụng hàm thay thế chuỗi sẵn ()

Phương pháp 2: Sử dụng hàm dịch () trong Python

example_string.replace(old_substring,new_substring,count).replace(old_substring,new_substring,count).replace(old_substring,new_substring,count)

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonis used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.

example_string="Python/programming/language"="Python/programming/language"="Python/programming/language"

Một slash (/) trong chuỗi python có thể được thay thế bằng một dấu gạch chéo ngược (\) bằng cách sử dụng hàm former setper (), phương thức dịch () hoặc biểu thức chính quy (re.sub ()).used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.=example_string.replace("/","\\")

print(new_string)(new_string)(new_string)

Output:

Nội dung\programming\language

1 sử dụng thay thế ()example”string because the second quotation marks have been rendered literal string character by the escape character -the backslash.returns a copy of the string after making the replacement, and therefore, the example_string variable will still have the original string even after execution.

Phương pháp 2: Sử dụng hàm dịch () trong Python

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonis used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.allows one to replace one or multiple characters which should be provided in a dictionary as shown below. The code replaces forward-slash (/) and “e” in a string with a backslash.

stringn="book/pencil/ink/pen/rubber"="book/pencil/ink/pen/rubber"="book/pencil/ink/pen/rubber"

Một slash (/) trong chuỗi python có thể được thay thế bằng một dấu gạch chéo ngược (\) bằng cách sử dụng hàm former setper (), phương thức dịch () hoặc biểu thức chính quy (re.sub ()).used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.=stringn.translate(str.maketrans({'/':'\\',"e":"\\"}))

print(stringn1)(stringn1)(stringn1)

Output:

Nội dung\p\ncil\ink\p\n\rubb\r

1 sử dụng thay thế ()example”string because the second quotation marks have been rendered literal string character by the escape character -the backslash.keys for the translate dictionary must be a single character otherwise, you will run into an error.

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonis used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.

Một slash (/) trong chuỗi python có thể được thay thế bằng một dấu gạch chéo ngược (\) bằng cách sử dụng hàm former setper (), phương thức dịch () hoặc biểu thức chính quy (re.sub ()).used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.re package works with regular expressions to detect patterns in strings. The package has a function sub() that can be used to find and substitute substrings. For example,

Backslash trong Pythonrere

string1="Python/programming/language/3.10"="Python/programming/language/3.10"="Python/programming/language/3.10"

Nội dung=re.sub("/","\\\\",string1)

print(string2)(string2)(string2)

Output:

1 sử dụng thay thế ()example”string because the second quotation marks have been rendered literal string character by the escape character -the backslash.\programming\language\3.10

2 sử dụng hàm dịch ()re module, we need to pass “\\\\” as the pattern to capture a single backslash.

Phương pháp 1: Sử dụng hàm thay thế chuỗi sẵn ()

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonraw string formatting (they are strings preceded by r), which converts backslash into a literal string. Note that, in the code, we still use “\\”, because the raw string only applies to the re pattern but we still need to write backslash as “\\”

Sử dụng hàm dịch ()=example_string.replace("/","\\")re

string1="Python/programming/language/3.10"="Python/programming/language/3.10"="Python/programming/language/3.10"

Sử dụng biểu thức chính quy (re.sub ())\programming\language=re.sub("/",r"\\",string1)

print(string2)(string2)(string2)

Output:

Backslash trong Pythonreturns a copy of the string after making the replacement, and therefore, the example_string variable will still have the original string even after execution.\programming\language\3.10

Phương pháp 3: Sử dụng biểu thức chính quy (re.sub ()) trong Pythonallows one to replace one or multiple characters which should be provided in a dictionary as shown below. The code replaces forward-slash (/) and “e” in a string with a backslash.

Một slash (/) trong chuỗi python có thể được thay thế bằng một dấu gạch chéo ngược (\) bằng cách sử dụng hàm former setper (), phương thức dịch () hoặc biểu thức chính quy (re.sub ()).=stringn.translate(str.maketrans({'/':'\\',"e":"\\"}))using the String replace() function, translate() method, or regular expression(re. sub()).

Nội dung\p\ncil\ink\p\n\rubb\r

Sử dụng phương thức str.Replace () để loại bỏ các vết chém phía trước khỏi một chuỗi, ví dụ:new_string = chuỗi.thay thế('/', '') .replace() method to remove the forward slashes from a string, e.g. new_string = string. replace('/', '') . replace() method to remove the forward slashes from a string, e.g. new_string = string. replace('/', '') .

Python có sử dụng dấu gạch chéo hay ngược không?

Ngôn ngữ lập trình, chẳng hạn như Python, coi một dấu gạch chéo ngược (\) là một nhân vật thoát.Chẳng hạn, \ n đại diện cho nguồn cấp dữ liệu và \ t đại diện cho một tab.Khi chỉ định một đường dẫn, một dấu gạch chéo về phía trước (/) có thể được sử dụng thay cho một dấu gạch chéo ngược.Hai dấu gạch chéo ngược có thể được sử dụng thay vì một để tránh lỗi cú pháp.a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.

Làm cách nào để thay đổi dấu gạch chéo ngược trong Python?

Sử dụng phương thức str.Replace () để thay thế một dấu gạch chéo ngược bằng một dấu gạch chéo ngược duy nhất, ví dụ:new_string = chuỗi.thay thế('\\\\', '\\') .Backslashes có một ý nghĩa đặc biệt trong Python, vì vậy mỗi dấu gạch chéo ngược phải được thoát ra với một dấu gạch chéo ngược khác.replace() method to replace a double backslash with a single backslash, e.g. new_string = string. replace('\\\\', '\\') . Backslashes have a special meaning in Python, so each backslash has to be escaped with another backslash. replace() method to replace a double backslash with a single backslash, e.g. new_string = string. replace('\\\\', '\\') . Backslashes have a special meaning in Python, so each backslash has to be escaped with another backslash.