def gen_word(n): ''' 生成一个长度为n的随机单词 :param n: :return: ''' word = [] for i in range(n): rand_idx = t_random.randint(0, len(string.ascii_letters)) word.append(string.ascii_letters[rand_idx]) return ''.join(word)
# -*- coding:utf8 -*- import MySQLdb import t_random import string def gen_word2(n): return "".join([t_random.choice(string.ascii_letters + string.digits) for i in range(n)]) __author__ = "ztj" if __name__ == "__main__": db = MySQLdb.connect(host="127.0.0.1", user="******", passwd="nn123456", db="test", charset="utf8") c = db.cursor() sql = "insert into test_src_talk (username, talk, type) values ('%s', '%s', %d);" for i in range(1000000): if i % 100000 == 0: print i username = gen_word2(20) talk = gen_word2(200) ttype = t_random.randint(1, 9) c.execute(sql % (username, talk, ttype)) db.commit() db.close()
def test_random_int(): print t_random.randint(1, 20) print t_random.randrange(1, 100, 3)