0

Can anyone help me out what I did wrong. So i need to print out the name read from file in a column of list like this:

admin

admin

admin

....

admin

But my code keep printing out the name sideways like this:

['admin', 'admin', 'admin', 'admin', 'admin']

Any help would really appreciate.

# Define a function

def parse_file():
    users = []
    filename = "auth.log"
    file = open(filename, 'r')
    string = "invalid user"

# Parsing auth.log file, detect invalid users, sort them and return the result

    for i in file:
        i = i.lower()
        pos = i.find(string)
        if pos >= 0:
            index = pos + len(string) + 1
            user = i[index:].split(" ")[0]
            users.append(user)
    users.sort()
    return users


if __name__ == "__main__":
    print(parse_file( ))
0

2 Answers 2

2

The print command works as it should. It prints a list of strings.

You probably want to do the following

>>> l = ['admin', 'admin', 'admin', 'admin', 'admin']
>>> print('\n'.join(l))
admin
admin
admin
admin
admin

See str.join(iterable) for the function that was "missing".

Sign up to request clarification or add additional context in comments.

Comments

2

It is just printing your users list. A list has the form of ['a', 'b', ...] You can just do:

for x in parse_file():
    print(x)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.