Ataman
Ataman

Reputation: 2590

Replacing special characters in Python

How can I replace these characters in Python2.7 with comma:

| •

something like this does not work:

a= b.replace("|", ",")

Thanks

Upvotes: 0

Views: 1482

Answers (1)

Mudassir Hasan
Mudassir Hasan

Reputation: 28751

Use Regular expression which contains list of characters to be replaced

import re
a = re.sub(u'[|•]', ',', a)

SYNTAX:

re.sub(pattern, repl, string, max=0)

This method replace all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided.

EDIT You have to declare at the top of source file that it uses Unicode literals.

# -*- coding: utf-8 -*-

Also prefix string being searched with u

a = u"6• 918417•12"
a = re.sub(u"[|•]", ",", a)

Upvotes: 3

Related Questions