12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import time
- import json
- import os
- from random import choice
- def generate_person():
- name = ''
- tel = ''
- letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', ]
- nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
- while len(name) != 5:
- name = name + choice(letters)
- while len(tel) != 7:
- tel = tel + str(choice(nums))
- return {"name": name, "tel": tel}
- def time_watcher():
- print('--- %s seconds ---' % (time.time() - start_time))
- def get_persons():
- persons = []
- for i in range(9):
- persons.append(generate_person())
- return persons
- def create_json(persons):
- path_f = os.path.dirname(os.path.abspath(__file__))
- with open(os.path.join(path_f, "create_persons.json"), 'w', encoding='utf-8') as json_file:
- json.dump(persons, json_file, indent=2, ensure_ascii=False)
- def write_json(person_dict):
- path_f = os.path.dirname(os.path.abspath(__file__))
- try:
- data = json.load(
- open(os.path.join(path_f, "write_persons.json")))
- except:
- data = []
- data.append(person_dict)
- with open(os.path.join(path_f, "write_persons.json"), 'w', encoding='utf-8') as json_file:
- json.dump(data, json_file, indent=2, ensure_ascii=False)
- def main():
-
-
- for i in range(9):
-
- write_json(generate_person())
- if __name__ == '__main__':
- start_time = time.time()
- main()
- time_watcher()
|