例如,我想使用python从文本文件中取出前30%的数据。从txt文件中获取一定数量的数据
这里有一些代码我都试图产生两个新的文件,但我不知道如何利用数据的一定比例和他们写
这里是:
import sys
def NewFile(FileName):
open(FileName,'r')
print('Creating new text file')
#A for 70% training data
AFileName = 'A'+FileName
#B for 30% testing data
BFileName = 'B'+FileName
try:
afile = open(AFileName,'a')
bfile = open(BFileName,'a')
afile.close()
bfile.close()
except:
print('~~~~~~')
sys.exist(0)
percentae意味着如果有100行做你想要30线 –