#!/usr/bin/env python # 读取 def fetch(backend): result = [] with open(‘account.txt‘,‘r‘)as f: flag =False for line in f: if line.strip().startswith("backend") and line.strip() == "backend " + backend: flag = True continue if line.strip().startswith("backend"): flag = False break if flag and line.strip(): result.append(line.strip()) return result user = input("请选择:") ret = fetch(user) print(ret) #写入 def add(backend,record): record_list = fetch(backend) if not record_list: #backend不存在 with open(‘b.txt‘,‘r‘)as old, open(‘new.conf‘,‘w‘)as new: for line in old: new.write(line) new.write(‘backend ‘ + backend + ‘\n‘) new.write(‘ ‘ * 8 + record + ‘\n‘) else: # backend已存在 if record in record_list: import shutil shutil.copy(‘b.txt‘,‘new.conf‘) pass else: #backed存在 record不存在 record_list.append(record) with open(‘b.txt‘,‘r‘)as old,open(‘new.conf‘,‘w‘)as new: flag = False for line in old: if line.strip().startswith(‘backend‘) and line.strip() == ‘backend ‘ + backend: flag = True new.write(line) #写标题 for new_line in record_list: new.write(‘ ‘ * 8 + new_line + ‘\n‘) if flag and line.strip().startswith(‘backend‘): flag = False new.write(line) continue #防止重复写标题 if line.strip() and not flag: #后面不是标题的 new.write(line)
json
# 转换 字典列表 不支持元祖为基本数据类型 注意:字符串 必须为"" 不支持‘‘
import json
s = ‘{"k1":"v1"}‘
n = json.loads(s)
print(type(n),n)
时间: 2024-10-07 06:23:27