python Python中随机生成手机号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中随机生成手机号相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
#-*-coding:utf-8-*-
#@Author : Boris Hou
'''
生成随机的手机号
'''
import json, random
def getfilelines(filename, eol='\n', buffsize=4096):
#count how many lines
with open(filename, 'rb') as handle:
linenum = 0
buffer = handle.read(buffsize)
while buffer:
linenum += buffer.count(eol)
buffer = handle.read(buffsize)
return linenum
def readtline(filename, lineno, eol="\n", buffsize=4096):
#read certain lines
with open(filename, 'rb') as handle:
readedlines = 0
buffer = handle.read(buffsize)
while buffer:
thisblock = buffer.count(eol)
if readedlines < lineno < readedlines + thisblock:
# inthisblock: findthe line content, and return it
return buffer.split(eol)[lineno - readedlines - 1]
elif lineno == readedlines + thisblock:
# need continue read line rest part
part0 = buffer.split(eol)[-1]
buffer = handle.read(buffsize)
part1 = buffer.split(eol)[0]
return part0 + part1
readedlines += thisblock
buffer = handle.read(buffsize)
else:
raise IndexError
def getrandomline(filename):
#read random line
import random
return readtline(
filename,
random.randint(0, getfilelines(filename)),
)
def createPhone():
prelist=["130","131","132","133","134","135","136","137","138","139","147","150","151","152","153","155","156","157","158","159","186","187","188"]
return random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
if __name__ == '__main__':
null,positive,negative = [],[],[]
phoneNumberList, timeList, count = [], [], 0
f = file('D:/2.json', 'w')
#null
for i in range(0):
n1 = getrandomline("D:/null.csv")
null.append(n1.replace('\r',''))
print "null ones: " + n1
#positive
for i in range(5):
p = getrandomline("D:/positive.csv")
positive.append(p.replace('\r',''))
print "positive ones: " + p
#negative
for i in range(5):
n = getrandomline("D:/negative.csv")
negative.append(n.replace('\r',''))
print "negative ones" + n
#fake phone number
for i in range(100):
phoneNumberList.append(createPhone())
phoneNumberList.extend(null)
phoneNumberList.extend(positive)
phoneNumberList.extend(negative)
for i in range(len(phoneNumberList)):
timeList.append(random.randint(1,234))
#create and dump data
print len(phoneNumberList)
print dict(zip(phoneNumberList, timeList))
json.dump(dict(zip(phoneNumberList,timeList)),f)
f.close()
以上是关于python Python中随机生成手机号的主要内容,如果未能解决你的问题,请参考以下文章