程序留空行

时间:2013-08-02 02:36:34

标签: python file line

这是我用来解密一次打击垫的python程序,但程序在解码文件的顶部留下一个空白行,我不知道为什么它离开了行,但我知道它是与我如何使用顶线相关,我使用顶行来存储文件的名称,然后它用于为解密文本命名文件,然后顶行被删除,但我不知道如何获得摆脱文件中的空白行。

import os

q = 1
while q == 1: 
    #opens the cipher text and it converts it to decimal
    cipher = raw_input("cipher text: ")
    cipher1 = open(cipher, "r")
    cipher2 = cipher1.read()
    cipher3 = [ord(c) for c in cipher2]

    #opens the key and coverts it to decimal
    key = raw_input("key: ")
    key1 = open(key, "r")
    key2 = key1.read()
    key3 = [ord(c) for c in key2]

    #subtracts the key from the cipher
    a = cipher3
    b = key3
    c = map(lambda x: (x[0]-x[1]) % 256, zip(a,b))

    #prints out the decrypted plain text
    decrypt = ''.join(map(chr,c))

    string1 = decrypt.index('\n')
    name = decrypt[0:string1]

    #makes a file with the decrypted output
    path1 = raw_input("out folder: ")
    path2 = path1 + "/" + name

    string3 = decrypt.index('\n')
    length = len(decrypt)
    decrypt = decrypt[string1:length]

    if os.path.exists(path2):
        f1 = file(path2, "a")
        f1 = open(path2, "a")
        f1.write(decrypt)
        f1.close()
    else:
        f1 = file(path2, "w")
        f1 = open(path2, "w")
        f1.write(decrypt)
        f1.close()
    print 50*"-"

2 个答案:

答案 0 :(得分:2)

您将string1指向行尾。

string1 = decrypt.index('\n')

但如果这一行你需要从下一个字符切片:

decrypt = decrypt[string1:length]

答案 1 :(得分:1)

更改

decrypt = decrypt[string1:length]

decrypt = decrypt[string1+1:length]