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:
Code language: PHP [php]
print['Hello,\n World']
Output:
Hello, World
\ N là một ký tự duy nhất, không phải hai. Ví dụ:
Code language: PHP [php]
s = '\n' print[len[s]] # 1
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:Code language: PHP [php]
s = '"Python\'s awesome" She said' print[s]
Output:
Code language: JavaScript [javascript]
"Python's awesome" She said
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:
Code language: PHP [php]
colors = ['red','green','blue'] s = f'The RGB colors are:\n {'\n'.join[colors]}' print[s]
Error:
Code language: JavaScript [javascript]
SyntaxError: f-string expression part cannot include a backslash
Để 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[]
0Output:
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[]
1Dấ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[]
2Output:
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[]
3Bản tóm tắt
- Nhân vật Backslash Python [
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.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[]
- Sử dụng Backslash Python [
6] để thoát khỏi các ký tự đặc biệt khác trong mộ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[]
- 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
3.Hello \\\"Bob\\\"
- 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?