试图删除字符 EM DASH “—"(–) 在 python 中使用正则表达式 [英] Trying to remove character EM DASH "—" (—) in python using regex

查看:51
本文介绍了试图删除字符 EM DASH “—"(–) 在 python 中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用正则表达式删除这个顽固的 EM DASH 字符,但由于某种原因我无法让它工作.这是我正在使用的代码.

I got this stubborn EM DASH character that I'm trying to remove using regex, but for some reason I can't get it to work. This is the code I'm using.

editedSource = re.sub(r'\u2014','',str(source))

我在这里做错了什么?我很确定我得到了正确的字符代码.这是角色:

What am I doing wrong here? I'm pretty sure I got the right character code. Here's the character:

它看起来像

——"

.

谢谢!

推荐答案

Prep-end regex pattern with u 告诉正则表达式引擎解析 unicode 并且不要尝试强制转换 unicodestr.

Prep-end regex pattern with u to tell regex engine that parse the unicode and do not try to cast unicode into str.

>>>source = u'hello\u2014world'
>>>re.sub(ur'\u2014','',source)
>>>u'helloworld'

这篇关于试图删除字符 EM DASH “—"(–) 在 python 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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