1 class file_dir: 2 3 def __init__(self,p='dir.txt'): 4 import json 5 self.path = p 6 try: 7 with open(p,'a') as f: 8 self.date = json.load(f) 9 except Exception as e: 10 with open(p,'w') as f: 11 json.dump({},f) 12 self.date = {} 13 def setdir(self,*args): 14 for i in args: 15 b = '[%s]' % i 16 self.date.setdefault(b,{}) 17 b = b+b 18 import json 19 with open(self.path,'w') as f: 20 json.dump(self.date,f) 21 22 world = file_dir('world.txt') 23 world.setdir('亚洲','中国','陕西','西安','雁塔区','高新路')
文本结果如下:
{"[\u4e9a\u6d32]": {}, "[\u4e2d\u56fd]": {}, "[\u9655\u897f]": {}, "[\u897f\u5b89]": {}, "[\u96c1\u5854\u533a]": {}, "[\u9ad8\u65b0\u8def]": {}}
本人新手,写的代码乱码了。
不明白具体哪个步骤出的问题,请大神赐教。万分感谢