如何用python中的单个反斜杠替换双反斜杠? [英] How to replace a double backslash with a single backslash in python?

查看:2376
本文介绍了如何用python中的单个反斜杠替换双反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串。该字符串是双反斜杠。我想用单反斜杠替换双反斜杠,以便unicode char代码可以正确解析。

 (Pdb)p fetched_pa​​ge 
'< p style =text-align:center; align =center>< strong>< span style =font-family:\Times New Roman\',serif; font-size:115%;> Chapter 0< \\ / span>< \\ / strong>< \\ / p> \\\
< p>< span style =font-family:\Times New Roman\',serif; font -size:115%;> Code\\\\中的\\Dreaming中的第0章给出了早期编程的简要描述,以及程序员今天如何和为什么仍然在努力...'

在此字符串的内部,您可以看到转义的unicode字符代码,例如:

  \\\\ 

我想把它变成:

  \\\ 

尝试1:

  fetched_pa​​ge.replace ('\\\\\','\\')

但这不工作 - 它搜索四重杆kslashes。



尝试2:

  fetched_pa​​ge.replace('\\','\')

但是,线路错误的结束。



尝试3:

 code> fetched_pa​​ge.decode('string_escape')

但这对文本没有影响。所有的双反斜杠仍然是双反斜杠。

解决方案

您可以尝试 codecs.escape_decode ,这应该解码转义序列。


I have a string. In that string are double backslashes. I want to replace the double backslashes with single backslashes, so that unicode char codes can be parsed correctly.

(Pdb) p fetched_page
'<p style="text-align:center;" align="center"><strong><span style="font-family:\'Times New Roman\', serif;font-size:115%;">Chapter 0<\\/span><\\/strong><\\/p>\n<p><span style="font-family:\'Times New Roman\', serif;font-size:115%;">Chapter 0 in \\u201cDreaming in Code\\u201d give a brief description of programming in its early years and how and why programmers are still struggling today...'

Inside of this string, you can see escaped unicode character codes, such as:

\\u201c

I want to turn this into:

\u201c

Attempt 1:

fetched_page.replace('\\\\', '\\')

but this doesn't work -- it searches for quadruple backslashes.

Attempt 2:

fetched_page.replace('\\', '\')

But this results in an end of line error.

Attempt 3:

fetched_page.decode('string_escape')

But this had no effect on the text. All the double backslashes remained as double backslashes.

解决方案

You can try codecs.escape_decode, this should decode the escape sequences.

这篇关于如何用python中的单个反斜杠替换双反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆