# encoding=utf-8
import random
import string
# 以追加方式開啟檔案,即保證後寫入的內容不會將之前的內容覆蓋
with
open('txt1.txt', 'a') as f:
# 生產20000條字串
for i in range(20000):
# 生成乙個隨機數
x = random.randint(10, 20)
# 利用sample方法生成字串,x代表長度
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, x))+'\n'
# 寫入檔案
f.write(ran_str)
Python隨機生成字串
背景 將不同型別的字串 大小寫字母 數字 特殊字元 中文 隨機放在一起,可以指定不同型別字串長度。如,全為大寫字母 15個數字 大小寫字母 數字 特殊字元 中文各1個 環境 windows7 python3.6.6 import string import random class randomst...
python生成隨機日期字串
生成隨機的日期字串,用於插入資料庫。通過時間元組設定乙個時間段,開始和結尾時間轉換成時間戳。時間戳中隨機取乙個,再生成時間元組,再把時間元組格式化輸出為字串 import time import random a1 1976,1,1,0,0,0,0,0,0 設定開始日期時間元組 1976 01 01...
python生成隨機日期字串
生成隨機的日期字串,用於插入資料庫。通過時間元組設定乙個時間段,開始和結尾時間轉換成時間戳。時間戳中隨機取乙個,再生成時間元組,再把時間元組格式化輸出為字串 encoding utf 8 import time import random a1 1976,1,1,0,0,0,0,0,0 設定開始日期...