A
A
animanshnik2019-06-25 11:49:20
Python
animanshnik, 2019-06-25 11:49:20

How to call one line from txt?

I don't understand how to get one line from text.txt, here is the code:

def admin(chatid, userid, text):
    print(2)
    if isadmin(chatid, userid):
        print(8)
        text[5:]
        if len(text.split()) > 1:
            candidat = text.split()[1].strip().lower()
            print(4)
            if candidat[:3] == '[id' and '|' in candidat:
                print(5)
                nid = candidat.split('|')[0][3:]
                if nid.isdigit():
                    print(6)
                    nid = int(nid)
                    hwid=('id'+str(nid))
                    print(hwid)
                    if hwid in open('text.txt').read():
                        send_message_to_chat(chatid, 'Айди уже существует в базе данных.')
                        return True
                        print('true')
        f=open('text.txt', 'a')
        f.write(hwid + '\n' )
        send_message_to_chat(chatid, 'Айди записан в базу администраторов')
    else:
        send_message_to_chat(chatid, 'Вы должны быть админом')

everything is written as you understand in text.txt in the format id + \n. when there is 1 ID, it's understandable, everything is fine, and excellent, but the point is that I have a check for the admin. In this case:
def isadmin(chatid, userid,):
    members_data = vk.messages.getConversationMembers(peer_id=2000000000 + chatid)
    for i in members_data['items']:
        if userid == i['member_id']:
            with open('text.txt', 'r') as file:
                #ids=file.readlines()
                while True:
                    id=file.read()
                    print(id)
                    print(id)
                    if not id:
                        break
                print(id)
                send_message_to_chat(chatid, 'id')
                if 'is_admin' in i or userid == id:
                    print('true')
                    return True
                else:
                    print('false')
                    return False

This code scans for the admin, and if everything is OK, it sends. And it has a feature that you can add the left id. So, when 1 ID, it fits in, and get hurt. But when there are 2+, they don't fit.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Yakushenko, 2019-06-25
@kshnkvn

I did not understand what you need, but you can read txt line by line like this: At the output, you will have a list in id. You can check for values ​​very simply:

>>> id = [1, 2, 3, 4, 5]
>>> if 5 in id:
...     print(True)
...
True

UPD. Just remove the while - you don't need it. read() reads the entire file at once.

A
antares4045, 2019-06-25
@antares4045

Are you looking for the readline method ?
PS Just be aware of the presence of \n and/or \r at the end of the string it returns
PPS Although for the greatest purity, the solution to your question should look something like this .
And ideally, immediately "make friends" with sqlite.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question