4

How can I replace the contents of strings with #'s in Python? Assume no comments, no multiple lines for one string. Like if there is a line in a python file:

print 'Hello' + "her mom's shirt".

This will be translated into:

print '#####' + "###############".

It's like a filter to deal with every line in a python file.

4
  • 3
    "The strings" in what exactly? Commented Sep 16, 2010 at 22:31
  • I mean the string in the python file. Like 'I'm superman', then it will be replace as '############', More straightly, just replace the content in the ' ' or " " by #s. Commented Sep 16, 2010 at 22:35
  • 4
    Please read, for the benefit of everyone: catb.org/esr/faqs/smart-questions.html#beprecise Commented Sep 16, 2010 at 22:37
  • 1
    This can't be done (reliably) using regex only. You'll have to parse the code. Commented Sep 16, 2010 at 22:43

3 Answers 3

5

If you're using Python and the thing you're parsing is Python there's no need to use regexp since there's a built-in parser.

Sign up to request clarification or add additional context in comments.

Comments

2
>>> import re
>>> s="The Strings"
>>> s=re.sub("\w","#",s)
>>> s
'### #######'
>>> s='Hello' + "her mom's shirt"
>>> s
"Helloher mom's shirt"
>>> re.sub("\w","#",s)
"######## ###'# #####"

----Edit

OK, Now I understand that you want the output to be from a Python file. Try:

import fileinput
import re 

for line in fileinput.input():
    iter = re.finditer(r'(\'[^\']+\'|"[^"]+")',line)
    for m in iter:
       span = m.span()
       paren = m.group()[0]
       line = line[:span[0]]+paren+'#'*(span[1]-span[0]-2)+paren+line[span[1]:] 
       print line.rstrip()

This does not deal with line breaks, the """ form, and is only tested again 1 or two files I have...

In general, it is better to use a parser for this kind of job.

Best

5 Comments

What I expect is just the characters in string will be replaced, but not the result after combination of the two strings.
@antonio081014: Based on the responses you are getting, it should be very clear to you that your question is not clear. You said in your post "It's like a filter to deal with every line in a python file." Are you saying you want to process a group of Python source files in another script?
@drewk: What I want is write this python script to deal with python file.
I think the question is quite clear now that he added the examples.
@drewk: Thank you very much. I finally got it. I really appreciate.
1

Don't need a regex to replace "I'm superman" with "############"

Try

input = "I'm superman"
print "#" * len(input)

4 Comments

Thank you for your help, but the case is I need to practice my python skill
Well if you wanted to do above with a regex it would be something like `print re.sub('.', '#', input)
you're missing the point. The source is supposed to be a python file from which the strings are to be parsed.
@AaronMcSmooth, yes the question changed while I was writing my answer. You got to be quick round here. I was responding to antonio's comment on his own question at the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.