从文件里随机抽取XX行

#! /usr/bin/env python #coding=utf-8 import codecs import random randomdata = range(0,97281) randomlist = random.sample(randomdata,500) print(len(randomlist)) FileRead = codecs.open('result.txt','r','utf-8') FileWrite = codecs.open('result.out','w','utf-8') randomlist =set(randomlist) num = 0 for eachline in FileRead.readlines(): num = num + 1 eachline = eachline.strip('/n').strip('/r') if eachline != '' :#去除空行 if num in randomlist: FileWrite.write(eachline+'/r/n') else: continue  

你可能感兴趣的:(python)