简体   繁体   中英

How to erase specific data in file python

I cant seem to write each line in a file so Its like a one long string like : "a,1;b,2;c,3". I want to write a method that deletes "b,2" for example. Also, I cant write it with lines and that why im a bit confused and stuck... Thnks for all the helpers.

class Data:

def __init__(self):

    print "are you ready?? :)"

def add_data(self, user_name, password):

    add = open("user_data.txt", "a")
    add.write(user_name + "," + password + ";")
    add.close()

def show_file(self):

    file = open("user_data.txt", "r")
    print file.read()
    file.close()

def erase_all(self):

    file = open("user_data.txt", "w")
    file.write("")
    file.close()

def return_names(self):

    file = open("user_data.txt", "r")
    users_data = file.read()
    users_data = users_data.split(";")
    names = []

    for data in users_data:

        data = data.split(",")
        names.append(data[0])

    file.close()
    return names

def is_signed(self, user_name):

    names = self.return_names()

    for name in names:

        if user_name == name:

            return True

    return False

def is_password(self, user_name, password):

    file = open("user_data.txt", "r")
    users_data = file.read()
    users_data = users_data.split(";")

    for data in users_data:

        data = data.split(",")

        if data[0] == user_name:

            if data[1] == password:

                return True

    file.close()

    return False

def erase_user(self, user_name):

    pass

As mentioned in the comments, you'll want to include newlines each time you write a line to the file. Just a suggestion, to make file handling easier you may want to consider using with open() each time you access the file.

So altogether, for example for the first class methods:

def add_data(self, user_name, password):
        with open('user_data.txt', 'a') as add:
            add.write(user_name + ',' + password + ';')
            add.write('\n') # <-- this is the important new line to include

    def show_file(self):
        with open('user_data.txt') as show:
            print show.readlines()

... and similar for other methods.

As for the method that deletes a user entry from the file:

# taken from https://stackoverflow.com/a/4710090/1248974
def erase_user(self, un, pw):
    with open('user_data.txt', 'r') as f:
        lines = f.readlines()

    with open('user_data.txt', 'w') as f:
        for line in lines:
            user_name, password = line.split(',')[0], line.split(',')[1].strip('\n').strip(';')
            if un != user_name and pw != password:
                f.write(','.join([user_name, password]))
                f.write(';\n')

test:

d = Data()
d.erase_all()
d.add_data('a','1')
d.add_data('b','2')
d.add_data('c','3')
d.show_file()
d.erase_user('b','2')
print 'erasing a user...'
d.show_file()

output:

are you ready?? :)
['a,1;\n', 'b,2;\n', 'c,3;\n']
erasing a user...
['a,1;\n', 'c,3;\n']

confirm line entry was removed from textfile:

a,1;
c,3;

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM