簡體   English   中英

程序留空行

[英]program leaving blank 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*"-"

您將string1指向行尾。

string1 = decrypt.index('\n')

但如果此行,則需要從下一個字符中切出:

decrypt = decrypt[string1:length]

更改

decrypt = decrypt[string1:length]

decrypt = decrypt[string1+1:length]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM