从CSV文件中删除行

时间:2018-09-04 20:47:18

标签: python-3.x csv delete-row

我仍然绝对是一个初学者。我正在为我的计算机科学入门课程设计一个项目,但只停留在一部分上。根据用户输入从CSV文件中删除网站和密码。我尝试了许多解决方案,但还是一片空白。提供了一些代码。我知道还有其他部分未完成。我只是在寻求帮助,以便能够从samplePasswords.csv文件中删除条目。

谢谢

    import csv
import sys

# The password list - We start with it populated for testing purposes
passwords = []


# The password file name to store the passwords to
passwordFileName = "samplePasswords.csv"

# The encryption key for the caesar cypher
encryptionKey=16

# Caesar Cypher Encryption

def passwordEncrypt (unencryptedMessage, key):

    # We will start with an empty string as our encryptedMessage
    encryptedMessage = ''

    # For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
    for symbol in unencryptedMessage:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            encryptedMessage += chr(num)
        else:
            encryptedMessage += symbol

    return encryptedMessage

def loadPasswordFile(fileName):

    with open(fileName, newline='') as csvfile:
        passwordreader = csv.reader(csvfile)
        passwordList = list(passwordreader)

    return passwordList


def savePasswordFile(passwordList, fileName):

    with open(fileName, 'w+', newline='') as csvfile:
        passwordwriter = csv.writer(csvfile)
        passwordwriter.writerows(passwordList)



while True:
    print("What would you like to do:")
    print(" 1. Open password file")
    print(" 2. Lookup a password")
    print(" 3. Add a password")
    print(" 4. Save password file")
    print(" 5. Print the encrypted password list (for testing)")
    print(" 6. Delete A Password")
    print(" 7. Change the Encryption Key")
    print(" 8. Exit The Program ")
    print("Please enter a number (1-6)")
    choice = input()

    if(choice == '1'): #Load the password list from a file
        passwords = loadPasswordFile(passwordFileName)

    if(choice == '2'): #Lookup at password
        print("Which website do you want to lookup the password for?")
        for keyvalue in passwords:
            print(keyvalue[0])
        passwordToLookup = input()

        for password in list(passwords):
            if passwordToLookup in password:
                print("The Password For " + passwordToLookup + " is " + passwordEncrypt(password[1],
                                                                                        (int(encryptionKey) * -1)))

# I am not sure this was the easiest solution, however it was the best one i could.
    if choice == '3':
        print("What website is this password for?")
        website = input()
        print("What is the password?")
        unencryptedPassword = input()
        encpass = passwordEncrypt(unencryptedPassword, int(encryptionKey))
        newencripass = ''.join(encpass)
        passwords.append((website, str(newencripass)))
        print(website, str(newencripass))

    if choice == '4': # Save the passwords to a file
            savePasswordFile(passwords,passwordFileName)

    if choice == '5': # print out the password list
        for keyvalue in passwords:
            print(', '.join(keyvalue))

    if choice == '6':
        print("What website password would you like to delete?")

        with open(passwordFileName, 'r') as f:
            reader = csv.reader(f)
            writer = csv.writer(f)



    if choice == '8':  # quit our program
        sys.exit()

    print()
    print()

0 个答案:

没有答案