Python 正则表达式替换 \u2022 [英] Python regex replacing \u2022

查看:40
本文介绍了Python 正则表达式替换 \u2022的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的字符串:

raw_list = u'软件工程师,对新产品和创新产品充满热情.从在大型和快速发展的初创企业工作中获得的经验.专长 \u2022 语言和框架:JavaScript(Nodejs、React)、Android、Ruby on Rails 4、iOS(Swift) \u2022 数据库:Mongodb、Postgresql、MySQL、Redis \u2022 测试框架:Mocha、Rspec xxxx 其他:Sphinx、MemCached,厨师.

我正在尝试用一个空格替换 \u2022.

x=re.sub(r'\u2022', ' ', raw_list)

但它不起作用.我做错了什么?

解决方案

您正在使用带有 r 的原始字符串.这告诉 Python 从字面上解释字符串,而不是实际使用转义字符(例如 \n).

<预><代码>>>>r'\u2022''\\u2022'

你可以看到它实际上是一个双反斜杠.相反,您想使用 >>> u'\u2022' 然后它会起作用.

请注意,由于您正在进行简单的替换,因此您只需使用 str.replace 方法:

x = raw_list.replace(u'\u2022', ' ')

对于复杂的模式匹配,您只需要一个正则表达式替换.

This is my string:

raw_list = u'Software Engineer with a huge passion for new and innovative products. Experienced gained from working in both big and fast-growing start-ups.  Specialties \u2022 Languages and Frameworks: JavaScript (Nodejs, React), Android, Ruby on Rails 4, iOS (Swift) \u2022 Databases: Mongodb, Postgresql, MySQL, Redis \u2022 Testing Frameworks: Mocha, Rspec xxxx Others: Sphinx, MemCached, Chef.'

I'm trying to replace the \u2022 with just a space.

x=re.sub(r'\u2022', ' ', raw_list)

But it's not working. What am I doing wrong?

解决方案

You're using a raw string, with the r. That tells Python to interpret the string literally, instead of actually taking escaped characters (such as \n).

>>> r'\u2022'
'\\u2022'

You can see it's actually a double backslash. Instead you want to use >>> u'\u2022' and then it will work.

Note that since you're doing a simple replacement you can just use the str.replace method:

x = raw_list.replace(u'\u2022', ' ')

You only need a regex replace for complicated pattern matching.

这篇关于Python 正则表达式替换 \u2022的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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