python 随机排序TXT文本内容
按照txt文本中随机排序并逐渐输出某一行。(不重复)
#coding:utf-8
import re
import requests
import random
def write(text):
with open("result.txt","a") as f:
f.write(text+"\n")
def findSubStr(substr, str, i):
count = 0
while i > 0:
index = str.find(substr)
if index == -1:
return -1
else:
str = str[index+1:]
i -= 1
count = count + index + 1
return count - 1
if __name__ == "__main__":
with open("test.txt","r",encoding="utf-8") as f:
texts = f.readlines()
while(1):
if len(texts) != 0:
result = random.choice(texts)
account = result.strip()
print(account)
result_n = findSubStr(":", result, 2)
#result_1 = account[0:result]
line = account[result_n + 1::]
texts.remove(result)
else:
break