你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

造数据使用的一些函数

2021/12/3 8:00:35
import string
import random
#大小写转换,str.lower()转为全部小写、str.upper()转为全部大写
def lower1():
    print("请输入你要转换的大写内容:")
    a = input()
    b = str.lower(a)
    print(b)
#随机生成9位数拼接生成邮件
def email9(n):
    emails = set()
    while len(emails) != n:
        email_start = random.choice("123456789")
        email_end = random.choice(('@163.com', '@qq.com', '@sina.com', '@126.com'))
        '''email_e = []
        for i in range(8):
            email_e.append(random.choice("0123456789"))
        '''
        email_e = [(random.choice("0123456789")) for i in range(8)]  # 这一句等于上面的循环
        email_old = list(email_start) + email_e
        email = ''.join(email_old) + email_end + '\n'
        emails.add(email)
        with open('qq.txt', 'w') as fw:
            fw.writelines(emails)
# 1、写一个函数,这个函数的功能是,传入一个数字,产生N条邮箱
# 2、产生的邮箱不能重复,且邮箱前面的长度是6-12之间
# 3、产生的邮箱必须包含大写字母、小写字母、数字和特殊字符
# 4、邮箱后缀可以选择163.com、qq.com、sina.com、126.com
def email(n):
    emails = set()
    while len(emails) !=n:
        email_len = random.randint(6,12)
        email_end = random.choice(('@163.com', '@qq.com', '@sina.com', '@126.com'))
        email_s = random.choice(string.ascii_lowercase) + random.choice(string.ascii_uppercase) + random.choice(
            string.punctuation) + random.choice(string.digits)#随机小写+随机大写+随机字符+随机数字
        str = string.digits + string.punctuation + string.ascii_letters#0-9+特殊字符+大小写
        #0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
        str_len = email_len - 4
        email_e = random.sample(str, str_len)  # 随机选取几个元素,返回list
        email_start = list(email_s) + email_e  # 字符串转list
        random.shuffle(email_start)  # 打乱列表,返回值为空
        email = ''.join(email_start) + email_end + '\n'  # 一个完整的邮箱号  list转字符串
        emails.add(email)
        print(emails)
    with open('user.txt', 'w') as fw:
        fw.writelines(emails)

#生成不重复的11位手机号码
def raddomPhone():
    headList = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139",
                "147", "150", "151", "152", "153", "155", "156", "157", "158", "159",
                "186", "187", "188", "189"]
    return (random.choice(headList) + "".join(random.choice("0123456789") for i in range(8)))
raddomPhone()
#生成0-100的随机分数
def score(n):
    scores = set()
    while len(scores) != n:
        score_e = [(random.choice("0123456789")) for i in range(2)]  # 这一句等于上面的循环
        score = ''.join(score_e) + '\n'
        scores.add(score)
        with open('scores.txt', 'w') as fw:
            fw.writelines(scores)

#转码,('ISO-8859-1')转中文
def r_new(r):
    r2 = r.encode('ISO-8859-1')
    r1 = r2.decode('utf8')
    print(r1)