0

I'm trying to create a code that cleans up a file I have so I can use it as data, but every time I run it, it freezes mid way, at "173529" every single time. The only reason I can think of is that I'm overworking my laptop and the console can't catch up or something. I can't see any reason or error that would cause such an issue, so I'm hoping somebody can identify it for me...

(Before all this is just a bunch of variables that edit the code and clean it up a bit before the main filtering process. The print codes are just there for debugging purposes. 'cn' is character number value, and 'nil' is number in list value)

import time
import re
import is
os.system("color")

 def spamp2():
    userList = []
    countertls = 0
    counterpus = 0
    a = open(document, "r+", encoding='utf8').read().replace("[breaker]", "")
    b = a.replace(" ","\n")
    c = b.replace(":heart:", "")
    d = c.replace(r":\w+:", "")
    e = d.replace(":", "\n")
    f = e.replace("  ", " ")
    g = f.replace("  ", " ")
    h = g.replace(" ", "")
    i = h.replace("", " ")
    j = i.replace("", "")
    k = j.replace(":", "")
    l = k.splitlines()
    m = [*set(l)]
    print("Running...")
    numinList = len(m)
    time.sleep(5)
    while numinList != 0:
        for i in m:
            if "" in i:
                ##  Character Number Value
                cn = len(i)
                ##  Number In List Value
                nil = len(m)
                if cn > 22:
                    m.remove(i)
                    ##countertls+1
                    print("\x1B[1mStringChar22+ Removed\x1B[0m")
                    print("CN Value = "+str(cn))
                    print("NuminList Value = "+str(nil)+"\n")
                    
                else:
                    userList.append(i)
                    m.remove(i)
                    ##counterpus+1
                    print("\x1B[1mPotentialUsername Added\x1B[0m")
                    print("CN Value = "+str(cn))
                    print("NuminList Value = "+str(nil)+"\n")

    else:
        print("Printing...")
        time.sleep(8)
        with open("tempy.txt", "a+", encoding='utf8') as tmp:
            tmp.truncate()
            tmp.write(userList)
            ##print("\nCharTLS Removed: "+countertls)
            ##print("CounterPUS Added: "+counterpus)
            print("\n   Proccess Completed  ")

Here's a sample of the data (Changed the usernames to conceal identity. It's just a big text file of a Discord log without any new lines):

Username: the problem with that was that it flew into the street    Nameuser: that's rought dude    Username: thankfully it didn't cause an accident otherwise it wouldn't be as fun [breaker]  but hooray for coke fireworks [breaker]  :partying_face:    

If anything else needs to be provided, please let me know.

DagerTCD
  • 23
  • 7
  • 2
    Why would `numinList > 0` ever become `false`? – PM 77-1 Sep 21 '22 at 19:45
  • It's hard to reproduce the problem. 1. We don't have the necessary imports. 2. `l` is not defined. 3. `userList` is not defined. 4. We don't have the file to process. On Stack Overflow we love [mre]s – Thomas Weller Sep 21 '22 at 19:46
  • Change `while numinList > 0:` to `while len(m) > 0:` – Barmar Sep 21 '22 at 19:49
  • There's no point in having an `else:` block on a loop if there's no `break` statement. Its only purpose is to distinguish between normal ending of the loop and exiting with `break`. – Barmar Sep 21 '22 at 19:50
  • 1
    Don't modify the list `m` while you're in a `for i in m:` loop. See https://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list-while-iterating-over-it – Barmar Sep 21 '22 at 19:52
  • BTW: there is no need to give each thing a new name. If you never use something again, you can just write `a = a.replace(...)` and then `a = a.replace(...)` again – Thomas Weller Sep 21 '22 at 20:05

2 Answers2

0

It seems like you're not recalculating numinList inside the while loop so it never decreases. Therefore, you have an infinite loop.

Also it would be helpful to know more about what the data looks like.

abinitio
  • 96
  • 5
0

You seem to be making this way more complicated than necessary.

m = [name for name in m if "" in name]
userList = [name for name in m if len(name) > 22]
print("Printing...")
os.sleep(8)
with open("tempy.txt", "w", encoding='utf8') as tmp:
    tmp.write("\n".join(userList) + "\n")

Use w mode when opening the file to truncate automatically before writing. And the argument to tmp.write() has to be a string, not a list.

Barmar
  • 741,623
  • 53
  • 500
  • 612