python - 如何从python中的字符串中删除反斜杠
问题描述
如果我使用替换功能,那么我也会收到错误消息。
filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")
filename_replace = filename.replace('\ ', " ")
错误说:
Anomalous backslash in string: '\ '. String constant might be missing an r prefix.
解决方案
您需要将反斜杠转义为\\
.
filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")
# say it's something like "c:\myfiles\test.txt"
filename_replace = filename.replace("\\"," ")
# becomes "c: myfiles test.txt"
您可以在此处阅读有关转义字符和字符串文字的更多信息: https ://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
推荐阅读
- react-native-fcm - Firebase 通知在使用一段时间后停止(react-native-fcm)
- c++ - 需要以空结尾的 string_view
- r - 用 OR 条件左连接两个 R 数据帧
- android - RecyclerView findViewHolderForLayoutPosition 和 findViewHolderForAdapterPosition 返回 null
- c++ - 从输入参数具体用例推导出的尾随返回类型
- java - 接口,抽象类还是别的什么?
- java - 试图了解包含的封装示例问题
- jquery - 在剃刀中将迭代数据从按钮传递到模态
- google-assistant-sdk - Google Home 不再广播来自 SDK 的消息 (10/17/2019)
- java - 如何在一段时间后自动更改设备壁纸?