Function strip () in python malfunction

0

I have a list in a file, where each line has a user agent (with "at the beginning and at the end of each line), which is used in a later part of a program, to perform automated tests using selenium. When I opened the list, I implemented that, for each line read, I would remove the first and last character from each line as follows:

USER_AGENTS_FILE = './users.txt'
RUNNING = True
    def LoadUserAgents(uafile=USER_AGENTS_FILE):
    uas = [ ]
    with open(uafile, 'rb' ) as uaf:
        for ua in uaf.readlines():
            if ua:
                uas.append(ua.strip()[1:-1-1])
    random.shuffle(uas)
    return uas 
uas = LoadUserAgents()

Adding also the feature to randomly pick rows. However, when I try to display the list by the following command:

for i in range(0,10):
print(random.choice(uas))

I get something like the following:

 b'Mozilla/5.0 (BeOS; U; BeOS BePC; en-US; rv:1.8.1.6) Gecko/20070731 BonEcho/2.0.0.'

When you actually need to get:

Mozilla/5.0 (BeOS; U; BeOS BePC; en-US; rv:1.8.1.6) Gecko/20070731 BonEcho/2.0.0.

Could anyone help me solve this problem?

    
asked by anonymous 10.04.2018 / 07:20

1 answer

0

Your string is in bytes format, and only needs to be decoded to a standard format.

>>> s =  b'Mozilla/5.0 (BeOS; U; BeOS BePC; en-US; rv:1.8.1.6) Gecko/20070731 BonEcho/2.0.0.'
>>> s.decode("utf-8")
'Mozilla/5.0 (BeOS; U; BeOS BePC; en-US; rv:1.8.1.6) Gecko/20070731 BonEcho/2.0.0.'
    
10.04.2018 / 13:17