循环在代码执行到一半时一直处于冻结状态



我正试图创建一个代码来清理我拥有的文件,这样我就可以将其用作数据,但每次运行它时,它都会在中途冻结,在"173529〃;每一次。我唯一能想到的原因是我的笔记本电脑工作过度,控制台无法跟上或其他什么。我看不出任何原因或错误会导致这样的问题,所以我希望有人能为我识别它…

(在此之前,这只是一堆变量,它们编辑代码并在主过滤过程之前对其进行清理。打印代码只是用于调试目的。"cn"是字符数值,"nil"是列表值中的数字(

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+ Removedx1B[0m")
print("CN Value = "+str(cn))
print("NuminList Value = "+str(nil)+"n")

else:
userList.append(i)
m.remove(i)
##counterpus+1
print("x1B[1mPotentialUsername Addedx1B[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  ")

以下是数据示例(更改用户名以隐藏身份。这只是Discord日志的一个大文本文件,没有任何新行(:

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:    

如果还有什么需要提供的,请告诉我。

似乎没有在while循环中重新计算numinList,因此它永远不会减少。因此,你有一个无限循环。

此外,了解更多有关数据的情况也会有所帮助。

您似乎让这种方式变得更加复杂。

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")

打开要在写入之前自动截断的文件时,请使用w模式。tmp.write()的参数必须是字符串,而不是列表。

最新更新