Py学习  »  Python

如何创建用户名/密码系统到.csv文件[python]

Stuart Little • 4 年前 • 898 次点击  

你好,我有点挣扎在这个python代码。基本上,我想让用户输入两个用户,用户1和用户2,他们都有密码(我希望密码是散列/加密的),但我不知道怎么做。 这是我的密码

#Asks for login
gameuniqueid = random.randint(1000,999999)

player1uniqueid = random.randint(1000,9999)
player1 = input("Enter player 1 name: ")
p1p = input("Enter/Create player 1 password: ")
p1_final_score = 0

player2uniqueid = random.randint(1000,9999)
player2 = input("Enter player 2 name: ")
p2p = input("Enter/Create player 2 password: ")
p2_final_score = 0  


print("Your game unique ID is:", gameuniqueid )
#Opens Files and enters information
game = []
user1 = []
user2 = []


file = csv.writer(open("database.csv", "a"))
rows.append(["Game id: ", gameuniqueid])
rows.append([player1uniqueid ,player1, p1p, p1_final_score])
rows.append([player2uniqueid ,player2, p2p, p2_final_score])
file.writerows()

所以需要的是csv文件中的每个游戏都有:

第1行:唯一ID:(游戏唯一编号)

第2行:第1列:P1用户名第2列:P1密码第3列:P1_最终得分

第3行:第1列:P2用户名第2列:P2密码3:P2最终得分

第4行:无

每次有人输入用户名和密码时,我都希望有一个这样的长列表。然后它将被添加到.csv中,就像我想要的那样,这样人们就可以查看他们的分数和详细信息。

我真的希望有人能帮助我,因为我有点挣扎。如果这没什么意义,请告诉我。

https://codeshare.io/G6oBZN

或者你只是想看看 https://pastebin.com/hdKFJj6s

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52778
 
898 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Alderven
Reply   •   1 楼
Alderven    5 年前

CSV的思想是在每一行存储相同的数据格式。这样地:

gameId,user1Id,user1Name,user1Pass,user1Score,user2Id,user2Name,user2Pass,user2Score

下面是代码:

import csv
import random
from passlib.apps import custom_app_context as pwd_context

game_id = random.randint(1000, 999999)
data = [game_id]
for i in range(2):
    data.append(random.randint(1000, 9999))
    data.append(input('Enter player {} name: '.format(i+1)))
    p_pass = input('Enter/Create player {} password: '.format(i+1))
    data.append(pwd_context.hash(p_pass))
    data.append(0)

print('Your game unique ID is: ', game_id)

with open('database.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(data)

输出 database.csv :

489042,6139,User1,$6$rounds=656000$YzvCUZobix403Zjm$ApMp4Zo7NzMFcvW3KRYwlcPl9OWHdGONCOQztZwdeUP/fc/6cZFPpIsREGVwvyGk30IqgYrJHq0I5NqBr1KIc.,0,8622,User2,$6$rounds=656000$x50kIsTvVHSBYqfM$/.TCk2qkNxbDRN5r1vyI5gPZ6YWJx5nL1gpUHdkc64trbAqbJHNDfHTJaEaieEE8Pt9IC45Vm4ueytYaMZ13u1,0