I'm trying to get a Python script that will take code from my clipboard and format it to be a VS Code snippet, finally place it back on my clipboard (via Pyperclip).
I want to escape
- Backslashes (\)
- Quotes (")
I want to replace
- Actual tabs with (\t)
Input:
import pyperclip
string = """def print_string():
print("YOLO\n")
"""
x = string.replace("\\", "\\\\").replace("\"","\\\"").replace("\t","\\t")
pyperclip.copy(x)
Actual Output: (Pasting from the clipboard)
def print_string():
print(\"YOLO
\")
Expected Output: (What would be ok to almost immediately be used in the body of a VS Code snippet)
def print_string():
\tprint(\"YOLO\\n\")
How do I get what I'm missing, encoding it a certain way?