Python 3.x 从CSV文件中删除行
我肯定还是个初学者。我正在为我的计算机科学入门课做一个项目,但我只专注于其中一个部分。根据用户输入从CSV文件中删除网站和密码。我已经尝试了很多解决方案,但还是一无所获。提供了一些代码。我知道还有其他部分没有完成。我只是想寻求帮助,以便能够从samplePasswords.csv文件中删除条目 多谢各位Python 3.x 从CSV文件中删除行,python-3.x,csv,delete-row,Python 3.x,Csv,Delete Row,我肯定还是个初学者。我正在为我的计算机科学入门课做一个项目,但我只专注于其中一个部分。根据用户输入从CSV文件中删除网站和密码。我已经尝试了很多解决方案,但还是一无所获。提供了一些代码。我知道还有其他部分没有完成。我只是想寻求帮助,以便能够从samplePasswords.csv文件中删除条目 多谢各位 import csv import sys # The password list - We start with it populated for testing purposes
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()
导入csv
导入系统
#密码列表-我们从为测试目的填充它开始
密码=[]
#用于存储密码的密码文件名
passwordFileName=“samplePasswords.csv”
#凯撒密码的加密密钥
encryptionKey=16
#凯撒密码
def passwordEncrypt(未加密的消息,密钥):
#我们将以一个空字符串作为加密消息开始
encryptedMessage=“”
#对于未加密消息中的每个符号,我们将在加密消息中添加一个加密符号
对于未加密消息中的符号:
如果symbol.isalpha():
num=ord(符号)
num+=键
如果symbol.isupper():
如果num>ord('Z'):
num-=26
elif numord('z'):
num-=26
elif num
那么您遇到了哪些问题?是否有任何错误消息?您尝试过什么,哪些有效/无效?我尝试过将其作为列表导入,然后执行“删除列表”,并尝试了在线找到的其他几种方法。我没有收到任何错误,但它不会将其从csv文件中删除。如果您将其作为列表导入并删除条目,请不要忘记您必须将文件写回磁盘。。。。