Python Challenge #2 Elimination -
i solver python challenge #2 microsoft word (find , replace) when used python, failed because code not work...
with open("c:\\python34\\python challenge\\q2\\q2.txt") 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: print out properties of file. why not information? , also, how can change it? tried f.open()
, "f". have changed code multiple times thoughout these few hours (i think 3) code still dead.
you need use f = f.read()
:
f = str(f)
show string representation of file object, like:
<open file 'c:\\python34\\python challenge\\q2\\q2.txt"', mode 'r' @ 0x7f76a5a26e40>
then use str.translate
remove characters:
with open("c:\\python34\\python challenge\\q2\\q2.txt") f: f = f.read() f = f.translate(none,"!@#$%&()*^") print(f)
for python 3 need created dict mapping using ord
of characters replace , pass translate:
with open("c:\\python34\\python challenge\\q2\\q2.txt") f: f = f.read() # {ord(ch):"" ch in "!@#$%&()*^"} f = f.translate({64: '', 33: '', 35: '', 36: '', 37: '', 38: '', 40: '', 41: '', 42: '', 94: ''}) print(f)
Comments
Post a Comment