Hướng dẫn python write single backslash to file - python ghi dấu gạch chéo ngược đơn vào tệp

Tôi đã có một tệp đầu vào với một số chuỗi chứa các trích dẫn kép trong đó và muốn tạo tệp tiêu đề kiểu C với Python.

Say,

input file: Hello "Bob"
output file: Hello \"Bob\"

Tôi không thể viết mã để có được một tệp như vậy, đây là những gì tôi đã thử cho đến nay:

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()

Tệp đầu ra luôn chứa:

Hello \\\"Bob\\\"

Tôi không gặp vấn đề gì khi viết chuỗi

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
4,
key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
5 vào tệp đầu ra.

Có vẻ như tôi chỉ có thể viết số hoặc hai dấu gạch chéo ngược, ai đó có thể giúp đỡ được không?

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về ký tự Backslash Python như là một phần của ký tự chuỗi đặc biệt hoặc để thoát khỏi các ký tự trong một chuỗi.: in this tutorial, you’ll learn about the Python backslash character as a part of a special sequence character or to escape characters in a string.

Giới thiệu về Backslash của Python

Trong Python, dấu gạch chéo ngược (____ 16) là một nhân vật đặc biệt. Nếu bạn sử dụng dấu gạch chéo ngược ở phía trước của một nhân vật khác, nó sẽ thay đổi ý nghĩa của nhân vật đó.

Ví dụ,

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
7 là một nhân vật theo nghĩa đen. Nhưng nếu bạn sử dụng ký tự dấu gạch chéo ngược ở phía trước chữ cái
key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
7, nó sẽ trở thành ký tự tab (
key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
5).

Nói chung, dấu gạch chéo ngược có hai mục đích chính.

Đầu tiên, ký tự dấu gạch chéo ngược là một phần của các chuỗi ký tự đặc biệt như ký tự tab

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
5 hoặc ký tự dòng mới
key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
4.

Ví dụ sau in một chuỗi có ký tự dòng mới:

print('Hello,\n World')

Code language: PHP (php)

Output:

Hello, World

\ N là một ký tự duy nhất, không phải hai. Ví dụ:

s = '\n' print(len(s)) # 1

Code language: PHP (php)

Thứ hai, dấu gạch chéo ngược (

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
6) thoát khỏi các ký tự đặc biệt khác. Ví dụ: nếu bạn có một chuỗi có một trích dẫn duy nhất bên trong một chuỗi được trích xuất như chuỗi sau, bạn cần sử dụng dấu gạch chéo ngược để thoát khỏi ký tự trích dẫn duy nhất:

s = '"Python\'s awesome" She said' print(s)

Code language: PHP (php)

Output:

"Python's awesome" She said

Code language: JavaScript (javascript)

Backslash trong F-String

PEP-498 chỉ định rằng một chuỗi F không thể chứa một ký tự dấu gạch chéo ngược như là một phần của biểu thức bên trong niềng răng xoăn

Hello \\\"Bob\\\"
3.

Ví dụ sau sẽ dẫn đến lỗi:

colors = ['red','green','blue'] s = f'The RGB colors are:\n {'\n'.join(colors)}' print(s)

Code language: PHP (php)

Error:

SyntaxError: f-string expression part cannot include a backslash

Code language: JavaScript (javascript)

Để khắc phục điều này, bạn cần tham gia các chuỗi trong danh sách

Hello \\\"Bob\\\"
4 trước khi đặt chúng vào niềng răng xoăn:

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
0

Output:

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
1

Dấu gạch chéo ngược trong chuỗi thô

Chuỗi thô coi ký tự dấu gạch chéo ngược (

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
6) là một đặc tính theo nghĩa đen. Ví dụ sau đây coi ký tự dấu gạch chéo ngược
key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
6 là một ký tự theo nghĩa đen, không phải là một nhân vật đặc biệt:

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
2

Output:

key = 'key'
val = 'Hello "Bob"'
buf_list = list()
...
val = val.replace('"', b'\x5c\x22')
# also tried: val = val.replace('"', r'\"')
# also tried: val = val.replace('"', '\\"')
buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
...
for keyval in buf_list:
    lang_file.write(keyval)
lang_file.close()
3

Bản tóm tắt

  • Nhân vật Backslash Python (
    key = 'key'
    val = 'Hello "Bob"'
    buf_list = list()
    ...
    val = val.replace('"', b'\x5c\x22')
    # also tried: val = val.replace('"', r'\"')
    # also tried: val = val.replace('"', '\\"')
    buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
    ...
    for keyval in buf_list:
        lang_file.write(keyval)
    lang_file.close()
    
    6) là một ký tự đặc biệt được sử dụng như một phần của chuỗi đặc biệt như
    key = 'key'
    val = 'Hello "Bob"'
    buf_list = list()
    ...
    val = val.replace('"', b'\x5c\x22')
    # also tried: val = val.replace('"', r'\"')
    # also tried: val = val.replace('"', '\\"')
    buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
    ...
    for keyval in buf_list:
        lang_file.write(keyval)
    lang_file.close()
    
    5 và
    key = 'key'
    val = 'Hello "Bob"'
    buf_list = list()
    ...
    val = val.replace('"', b'\x5c\x22')
    # also tried: val = val.replace('"', r'\"')
    # also tried: val = val.replace('"', '\\"')
    buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
    ...
    for keyval in buf_list:
        lang_file.write(keyval)
    lang_file.close()
    
    4.
  • Sử dụng Backslash Python (
    key = 'key'
    val = 'Hello "Bob"'
    buf_list = list()
    ...
    val = val.replace('"', b'\x5c\x22')
    # also tried: val = val.replace('"', r'\"')
    # also tried: val = val.replace('"', '\\"')
    buf_list.append((key + '="' + val + '";\n').encode('utf-8'))
    ...
    for keyval in buf_list:
        lang_file.write(keyval)
    lang_file.close()
    
    6) để thoát khỏi các ký tự đặc biệt khác trong một chuỗi.
  • Các chuỗi F không thể chứa dấu gạch chéo ngược một phần biểu thức bên trong niềng răng xoăn
    Hello \\\"Bob\\\"
    
    3.
  • Các chuỗi thô coi dấu gạch chéo ngược (\) là một đặc tính theo nghĩa đen.

Bạn có thấy hướng dẫn này hữu ích không?