python输出指定长度的字符串作者:luke

import io
import sys
import random
import string
def generate_random_str(randomlength=16):
  """
  生成一个指定长度的随机字符串,其中
  string.digits=0123456789
  string.ascii_letters=abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
  """
  str_list = [random.choice(string.digits + string.ascii_letters) for i in range(randomlength)]
  random_str = ''.join(str_list)
  return random_str
def generate_random_str2(randomlength=16):
  """
  生成一个指定长度的随机字符串
  """
  random_str = ''
  # base_str = '一二三四五六七八九十ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789'
  base_str = '一二三四五六七八九十'
  length = len(base_str) - 1
  for i in range(randomlength):
    random_str += base_str[random.randint(0, length)]
  return random_str
# notepad++ 下执行输出,不用这句输出是乱码,而jupyter notebook则没有问题。
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
f = generate_random_str2(65536)
print (f)
# print( u"学习")
时间:2020-5-27 分类:练习