我不确定为什么我的电脑在使用这个程序时会一直崩溃我尝试通过每次运行时关闭文件来编辑它但它仍然会崩溃。
f = open("testfile.txt", "r")
text = f.read()
text = str(text)
looper= text.count(",")
f.close()
god= open("dog.txt","w+")
god.write("Passwords")
god.close()
block=""
for count in range(looper):
first= (text.find(','))
second= (text.find(':'))
block = text[first:second]
text = text[:first] + text[second:]
god= open("dog.txt","a")
god.write(block)
god.close()
我正在尝试从文本文件中获取项目并将其格式化为新的文本文件。
编辑代码:
f = open("testfile.txt", "r")
text = f.read()
text = str(text)
looper= text.count(",")
f.close()
god= open("dog.txt","w+")
god.write("Testing")
god.close()
for count in range(looper):
block = ""
first= (text.find(':'))
second= (text.find(','))
block= text[first:second]
text = text[second:]
text = text[first:]
print (block)
god= open("dog.txt","a")
god.write(block)
god.close()
在代码的第一部分,我没有清除字符串块,而且我没有更新字符串文本,所以会发生的事情是每次循环阻塞都会自我复制
EX:
loop1) :helloworld
loop2) :helloworld:helloworld
loop3) :helloworld:helloworld:helloworld
使这两项修改解决了问题。