开发者

How to remove special characters from the beginning of a string in Python

I am getting my data from XML which may some time contain special Character at beginning like:

'This is a sample title or %&*I don't know if this is the text

I tried with : title[0].isstring() or title[0].isdigit() and then remove th开发者_开发知识库e character. But if there are more than one special character at the beginning, then how do I remove it? Do I need a for loop?


You could use a regular expression:

import re
mystring = re.sub(r"^\W+", "", mystring)

This removes all non-alphanumeric characters from the start of your string:

Explanation:

^   # Start of string
\W+ # One or more non-alphanumeric characters


>>> import re
>>> re.sub(r'^\W*', '', "%&*I don't know if this is the text")
"I don't know if this is the text"

#or

>>> "%&*I don't know if this is the text".lstrip("!@#$%^&*()")
"I don't know if this is the text"


If there are just a few specific kinds of characters you want to remove, use lstrip() ("left strip").

For instance, if you wanted to remove any starting %, &, or * characters, you'd use:

actual_title = title.lstrip("%&*")

On the other hand, if you want to remove any characters that aren't part of a certain set (e.g. alphanumerics), then the regex solution specified in Tim Pietzcker's solution is probably the easiest way.


Using a strip function to remove any special characters from the beginning and end of the string. Ex.

str = ").* this is text .("
str.strip(")(.* ")

Output: 'this is text'

If you want to remove from the beginning of string use lstrip() Ex.

str = ").* this is text .("
str.lstrip(")(.* ")

Output: 'this is text .('

If you want to remove from the end of the string use rstrip() Ex.

str = ").* this is text .("
str.rstrip(")(.* ")

Output: ').* this is text'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜