Skip to content Skip to sidebar Skip to footer

Excluding Lines From A Text File (python)

I recently posted about trying to get my code to exclude the words 'RT' and 'DM' when counting lines. What I am trying to achieve with this program is to read a number x from the u

Solution 1:

Change

wordlist = [r.split()[0] for r in f]

to

wordlist = [r.split()[0] for r in f if "DM" not in r and "RT" not in r]

Solution 2:

i think you are doing it to complicated maybe. If you just want the top x users you could do:

x = input("top x users: ")
users = []
with open("stream.txt","r") as sin:
    for line insin:
        words = line.split()
        ifnot"DM"in words andnot"RT"in words andnot words[0] in users:
            users.append(words[0])

print(users)

that outputs ['andrew', 'fred', 'judy'] for me.

Post a Comment for "Excluding Lines From A Text File (python)"