You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
766 B
34 lines
766 B
#!/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')
|
|
|