【发布时间】:2018-12-06 18:42:08
【问题描述】:
我的代码有问题。 它需要将每个字符更改为之前的字符。 这里的问题是文件:
uif xiffmt po uif cvt hp spvoe boe spvoe
uif xiffmt po uif cvt hp spvoe boe spvoe
它需要返回:
the wheels on the bus goes round and round
the wheels on the bus goes round and round
但它会在一行中返回它们:
the wheels on the bus goes round and round the wheels on the bus goes round and round
我该如何解决这个问题?
def decode(in_file, out_file):
try:
f = open(in_file, 'r')
for line in f:
lst = list(line)
lst2 = [chr(ord(x) - 1) if x != ' ' else ' ' for x in lst]
a = "".join(lst2)
with open('out_file.txt','a') as f2:
f2.write(a)
except IOError:
print "Cant decipher' {"+in_file+"} 'due to an IO Error."
f.close()
finally:
if f!=None:
f.close()
f2.close()
print decode( 'q4.txt', 'out_file.txt')
【问题讨论】:
-
换行符不知何故丢失了。您需要防止这种情况发生,或者在完成一行后将其添加回来。
标签: python-2.7 file ascii caesar-cipher