#!/usr/bin/python3 # -*- coding:utf-8 -*- import json def write_db(db, filepath): try: json.dump(db, open(filepath, 'w', encoding='utf8')) except (json.JSONDecodeError, IOError) as e: print(e.args) raise def text_to_db(txt_path): with open(txt_path, 'r', encoding='utf8') as txt_fb: db = [] while True: line = txt_fb.readline() if not line: break line = line.strip() db.append({'uid': line, 'role': 1, 'name': line}) return db def main(txt_path, db_path): write_db(text_to_db(txt_path), db_path) print('生成成功') if __name__ == "__main__": main('users.txt', r'doc\users.json')