I'm trying to implement Rot-13 function, but stuck with, I assume, new lines.
Here's my code:
import cgi, string
def convert():
lower = string.ascii_lowercase
upper = string.ascii_uppercase
punctuation = string.punctuation + ' '
with open('data.txt', 'r') as myfile:
s = myfile.read()
s = '%(pre_b)s%(s)s%(pre_e)s' % {'pre_b': '<pre>', 's': s, 'pre_e': '</pre>'}
s = ''.join(map(lambda x: shift(x, lower, upper, punctuation), s[5:-6]))
return cgi.escape(s, quote= True)
def shift(x, lower, upper, punctuation):
if x in punctuation:
return x
elif x.istitle():
return upper[(upper.index(x) + 13) % 26]
try:
return lower[(lower.index(x) + 13) % 26]
except:
print x
print convert()
One-line sentences are being processed OK, but when input contains new line, python says TypeError: expected string, NoneType found
Contents of the data.txt file is following:
test
test test
Please help.