def find(): txt = open(r'F:\send1.txt','rb') data = txt.read().decode('utf-8') #python3一定要加上這句不然會編碼報錯! txt.close() i = random.randint(1, 121) #我的文檔我數了一下是120行,這里就是1~121 ###得到對應的i行的數據 line=linecache.getline(r'F:\send1.txt',i)
或者升級一下:
def find(): txt = open(r'F:\send1.txt','rb') data = txt.read().decode('utf-8') #python3一定要加上這句不然會編碼報錯! txt.close()
#獲取txt的總行數! n = data.count('\n') print("總行數",n) #選取隨機的數 i = random.randint(1, (n+1)) print("本次使用的行數",i) ###得到對應的i行的數據 line=linecache.getline(r'F:\send1.txt',i)
順便提一下
如果你也喜歡Python 這里有一群Python愛好者匯集在此。
關注微信公眾號:【軟件測試技術】,回復 888,獲取QQ群號。
