马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想写一个数据生成的脚本,然后用下面代码生成的数据保存到CSV文件中为什么每行都会有一个换换行?
还有有其他方法写一个数据生成的脚本么,我感觉这个生成的还是不怎么方便使用,毕竟以后如果字段不一样,要求不一样,这个改起来感觉不是很优import random
import datetime
import time
import csv
dataCount = 10*10*100
codeRange = range(ord('a'),ord('z'))
alphaRange = [chr(x) for x in codeRange]
alphaMax = len(alphaRange)
daysMax = 42003
theDay = datetime.date(1990,1,1)
def genRandomName(nameLength):
global alphaRange,alphaMax
length = random.randint(1,nameLength)
name = ''
for i in range(length):
name += alphaRange[random.randint(0,alphaMax-1)]
return name
def genRandomDay():
global daysMax,theDay
mDays = random.randint(0,daysMax)
mDate = theDay + datetime.timedelta(days=mDays)
return mDate.isoformat()
def genRandomSex():
return random.randint(0,1)
def genDateBase(fileNmae,dataCount):
outp = file(fileNmae,'w')
writer = csv.writer(outp)
i = 0
while i < dataCount:
firstName = genRandomName(14)
lastName = genRandomName(14)
birthday = genRandomDay()
sex = genRandomSex()
# mLine = "%i,%s,%s,%s,%d\n" %(i+1,firstName,lastName,birthday,sex)
writer.writerow([i+1,firstName,lastName,birthday,sex])
i += 1
outp.close()
if __name__ == "__main__":
random.seed()
start = time.time()
genDateBase('db_test.csv',dataCount)
end = time.time()
print 'use time:%d'%(end-start)
print 'ok'
|