简体   繁体   中英

Python Challenge #2 Elimination

I solver the python challenge #2 with microsoft word (find and replace) but when I used python, I failed because this code would not work...

with open("C:\\Python34\\Python Challenge\\Q2\\Q2.txt") as f: 
    f = str(f)
    f = str.replace(f, '!','')
    f = str.replace(f, '@','')
    f = str.replace(f, '#','')
    f = str.replace(f, '$','')
    f = str.replace(f, '%','')
    f = str.replace(f, '^','')
    f = str.replace(f, '&','')
    f = str.replace(f, '*','')
    f = str.replace(f, '(','')
    f = str.replace(f, ')','')
    print(f)

Research: It will print out the properties of that file. Why not the information? And also, how can I change it? I tried f.open() and "f". I have changed the code multiple times already thoughout these few hours (I think 3) but this code is still dead.

You need to use f = f.read() :

f = str(f) will show the string representation of the file object, something like:

 <open file 'C:\\Python34\\Python Challenge\\Q2\\Q2.txt"', mode 'r' at 0x7f76a5a26e40>

Then use str.translate to remove the characters:

with open("C:\\Python34\\Python Challenge\\Q2\\Q2.txt") as f: 
    f = f.read()
    f = f.translate(None,"!@#$%&()*^")
    print(f)

For python 3 we need to created a dict mapping using the ord of the characters to replace and pass that to translate:

with  open("C:\\Python34\\Python Challenge\\Q2\\Q2.txt")  as f:
    f = f.read()
    # {ord(ch):"" for ch in "!@#$%&()*^"}
    f = f.translate({64: '', 33: '', 35: '', 36: '', 37: '', 38: '', 40: '', 41: '', 42: '', 94: ''})
    print(f)

When you're saying f = str(f) you're not reading f , you're getting the file object. A string of that, to be precise. Then you're replacing characters it doesn't have, and printing it out.

You need to make f be the content of the file:

with open("PythonChallenge2.txt") as f: 
    f = str(f.readlines())  ##<-- Changed this to actually set f to the file contents, not the file object
    f = str.replace(f, '!','')
    f = str.replace(f, '@','')
    f = str.replace(f, '#','')
    f = str.replace(f, '$','')
    f = str.replace(f, '%','')
    f = str.replace(f, '^','')
    f = str.replace(f, '&','')
    f = str.replace(f, '*','')
    f = str.replace(f, '(','')
    f = str.replace(f, ')','')
    print(f)

Adding this:

f = str.replace(f, '[','')
f = str.replace(f, ']','')
f = str.replace(f, '{','')
f = str.replace(f, '}','')
f = str.replace(f, '+','')
f = str.replace(f, '_','')
f = str.replace(f, '\\n','')

gives you an almost-obvious answer.

since no one else has mentioned it here is the re solution

  with open("C:\\Python34\\Python Challenge\\Q2\\Q2.txt") as f: 
      print re.sub("!@#$%&()*^","",f.read())

You can make a dictionary using your search and replace string.

Then search and replace each item in that dictioanry.

replace_dict = {'!':'', '@':'', '#':'', '$':'', '%':'', '^':'', '&':'', '*':'', '(':'', ')':''}

with open("C:\\Python34\\Python Challenge\\Q2\\Q2.txt") as f: 
    content = f.read() 
    print content         # <---print the original content
    for key,  value in replace_dict.items():
        content = content.replace(key, value)
    print content         # <---print the modified content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM