格式化的随机行数据写入本地文件,Python
import time
import datetime
import uuid
import random
l_type = ['IN', 'ER', 'WA']
file_name = 'test.txt'
if __name__ == '__main__':
with open(file_name, 'w', encoding='UTF-8') as f:
count = 1
while count < 10:
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
string = '[' + t + ']' + ' ' + str(random.choice(l_type)) + ' ' + '[SOMETYPE:' + str(
random.randint(1000, 10000)) + ']' + ' ' + str(
uuid.uuid4()) + '\n'
print(string)
f.write(string)
time.sleep(1)
count = count + 1
f.close()