有时候我们会需要大量数据来演示或者测试某些功能,但是可能无法获得这些数据,或者担心泄露信息而不想使用真实数据。这时,我们可以自己生成一些随机数据。
import random
import string
def getEmail():
# of course you can extend the following list
suffix = ['.com', '.org', '.net', '.cn']
characters = string.ascii_letters + string.digits + '_'
username = ''.join((random.choice(characters) for i in range(random.randint(6,12))))
domain = ''.join((random.choice(characters) for i in range(random.randint(3,6))))
return username + '@' + domain + random.choice(suffix)
def getTelNo():
return ''.join((str(random.randint(0,9)) for i in range(11)))
def getNameOrAddress(flag):
'''flag=1 means Name,
flag=0 meains Address'''
#[0x4e00, 0x9fa5] is the range of Chinese character code
start, end = 0x4e00, 0x9fa5
result = ''
#the length of most Chinese people's name is between 2 and 4
if flag==1:
rangestart, rangeend = 2, 5
#here suppose the address contains at most 30 Chinese Characters
elif flag==0:
rangestart, rangeend = 10, 30
else:
print('flag must be 1 or 0')
return
for i in range(rangestart, rangeend):
result += chr(random.randint(start, end))
return result
def getSex():
return random.choice(('男', '女'))
def getAge():
return str(random.randint(18,100))
def main(filename):
with open(filename, 'w', encoding='utf-8') as fp:
fp.write('Name,Sex,Age,TelNO,Address,Email\n')
#quickly generate information of 2000 persons
for i in range(2000):
name = getNameOrAddress(1)
sex = getSex()
age = getAge()
tel = getTelNo()
address = getNameOrAddress(0)
email = getEmail()
line = '{0},{1},{2},{3},{4},{5}\n'.format(name, sex, age, tel, address, email)
fp.write(line)
def output(filename):
with open(filename, 'r', encoding='utf-8') as fp:
for line in fp:
print(line.strip())
if __name__=='__main__':
filename = 'information.txt'
main(filename)
output(filename)