在文本文件的一行中为特定数字添加整数

时间:2017-10-11 10:44:49

标签: python

该文件包含用户姓氏年度会员身份状态预订和积分

成员用户名包括姓氏的前三个字母,加上一个三位数字,后跟当前年份的最后两位数字

我们目前正在努力寻找一种方法来为文本文件中的特定用户添加点数。

Booking = int(input('How many nights do you want to book:    '))
if Booking <= 14:
    Users = str(input('Have you already signed up as a user:    '))
    if Users == 'yes' or 'Yes':
        Login = str(input('Enter given username    '))
        datafile = open('Users2.txt')
        for line in datafile:
            if Login in line:
                points = Booking * 2500
                NewUser = Login
            else:

1 个答案:

答案 0 :(得分:0)

您可以在python下使用非常用户友好的sqlite。

import sqlite3
conn = sqlite3.connect('Users2.db') #this takes place of Users2.txt

c = conn.cursor()
# THIS ONLY FIRST TIME, JUST EXAMPLE
c.execute('''CREATE TABLE users
             (id int PRIMARY KEY NOT NULL,username text, surname text, year text, membership text)''') 
# you can create your structure here

c.execute("INSERT INTO users(id,username) VALUES (1,'farbiondriven')")


conn.commit()
Booking = int(input('How many nights do you want to book:    '))
if Booking <= 14:
    Users = str(input('Have you already signed up as a user:    '))
    if Users == 'yes' or 'Yes':
        Login = str(input('Enter given username    '))

        c.execute('SELECT * FROM users WHERE username=?', Login)
        outputQuery = c.fetchone()
        if(len(outputQuery)>0):
           ....do your stuff...




conn.close()