如何从文本文件中逐行读取变量并将其拆分为python

时间:2017-11-04 15:52:30

标签: python sqlite

我正在尝试创建一个注册菜单,其中包含您的用户名和密码,然后将它们保存到文本文本文件中的新行中,因此在生成多个帐户后,有多行,每行有一个用户名和一个密码。然后我想逐行读取文件,并将用户名和密码分成两个变量(u和p),将u和p保存到sqlite3中的数据库中,然后重复下一行,直到所有行都保存到数据库中。然而我无法弄清楚这一点,无论我尝试什么,将数值解包到很多或很少的错误。我认为每行末尾的\ n可能是/其中一个问题。 任何帮助表示赞赏谢谢。 这是代码:

import time
import sqlite3
import os


db2 = sqlite3.connect('pass.db')
cur = db2.cursor()

def sign_up():
    global cur

    details = '1'

    with open('username','a') as user:

        cur.execute('CREATE TABLE IF NOT EXISTS pass (username TEXT, password TEXT)')

        username = input('please enter a username:')
        username = username.title()


        password = ''
        password2 = '.'

        while password != password2 or len(password) < 6 or password.isalnum() == False:
            password = input('please enter a password with AT LEAST 6 CHARACTERS AND ONE NUMBER:')
            password2 = input('please confirm your password:')
        user.write(username + ',' + password + ',' + '\n')



    with open('username','r') as user:
        hello = ''.join(user.readlines())
        print (hello)

        for line in hello:
            details = user.readline()
            details = ''.join(details)
            print (details)
            u, p = details.split(',')
            cur.execute('INSERT INTO pass (username, password) VALUES (?, ?)',(u, p,)) 
        print(u)
        print(p)

    cur.execute('SELECT username FROM pass WHERE username = ?',(username,))
    for row in cur:
        print(row)

sign_up()

3 个答案:

答案 0 :(得分:0)

问题是您要加入hello中的行。当你迭代时,你正在迭代字符串中的字符,当然你不能分割一个字符。

答案 1 :(得分:0)

使用以下内容替换details = ''.join(details)代码

details = details.strip()
if details != '':
    u, p = details.split(',')            
    cur.execute('INSERT INTO pass (username, password) VALUES (?, ?)',(u., p,)) 

使用strip()或lstrip()函数删除&#39; \ n&#39;

答案 2 :(得分:0)

感谢大家,我知道这绝不是存储密码或其他任何东西的最好方法,但是它用于什么,我在google上试图找到更好的东西但是所有出现的东西要么是基本的,要么是在我还不了解的一个级别,密码的实际安全性对于使用它们的内容来说太重要了。

我用这段代码搞定了:

import time

导入sqlite3 import os

db2 = sqlite3.connect(&#39; pass.db&#39;) cur = db2.cursor()

def sign_up():     全球性的

details = '1'

with open('username','a') as user:

    cur.execute('CREATE TABLE IF NOT EXISTS pass (username TEXT, password TEXT)')

    username = input('please enter a username:')
    username = username.title()


    password = ''
    password2 = '.'

    while password != password2 or len(password) < 6 or password.isalnum() == False:
        password = input('please enter a password with AT LEAST 6 CHARACTERS AND ONE NUMBER:')
        password2 = input('please confirm your password:')
    user.write(username + ',' + password + ',' + '\n')



with open('username','r') as user:

    details = user.readline()
    details = details.strip()
    print(details)
    while details !=  '':
        if details != '':
            u, p = details.split(',',1)            
            cur.execute('INSERT INTO pass (username, password) VALUES (?, ?)',(u, p,))
            details = user.readline()
            details = details.strip()

cur.execute('SELECT * FROM pass')
for row in cur:
    print(row)
cur.execute('SELECT username FROM pass WHERE username = ?',(username,))
data = cur.fetchall()
if len(data) == 0:
    sign_up()

sign_up()