CSV文件一共有m(4017277)條數據,隨機取出n(10萬)條數據,保存值另外CSV一個文件。
注意:數據類型是DataFrame
import random from random import randint oldf=open('thp_zbwd_bing_01_Del_abs50.csv','r',encoding='UTF-8') newf=open('thp_zbwd_bing_01_Del_abs50_Random.csv','w',encoding='UTF-8') n = 0 # sample(x,y)函數的作用是從序列x中,隨機選擇y個不重復的元素 resultList = random.sample(range(0,4017277),100000) lines=oldf.readlines() for i in resultList: newf.write(lines[i]) oldf.close() newf.close()