赛雷feng
2016-10-20 20:12
import os import os.path import ConfigParser ''' 1: dump ini 2: del section 3: del item 4: modify item 5: add section 6: save modify ''' class student_info(object): def __init__(self,recordfile): self.logfile = recordfile self.cfg = ConfigParser.ConfigParser() def cfg_load(self): self.cfg.read(self.logfile) def cfg_load(self): self.cfg.read(self.logfile) def cfg_dump(self): se_list = self.cfg.section() print '=======================>' for se in cfg.section(): print se print cfg.items(se) print '=======================' def delete_item(self,section,key): self.cfg.remove_option(section,key) def delete_section(self,section): self.cfg.remove_section(section) def add_section(self,section): self.cfg.add_section(section) def set_item(self,section,key,value): self.cfg.set(section,key,value) def save(self): fp = open(self.logfile,'w') self.cfg.write(fp) fp.close() if __name__ = '__main__': info = student_info('imooc.txt') info.cfg_load() info.cfg_dump() info.set_item('userinfo','pwd','abc') info.cfg_dump() info.add_section('login') info.set_item('login','2016-10-20','20') info.cfg_dump() ingo.save()
这段代码哪错了,老是报错:
def delete_item(self,section,key): ^ IndentationError: unexpected indent
缩进肯定是4个空格没错
def cfg_dump(self): se_list = self.cfg.section() # se_list = self.cfg.sections() print '=======================>' for se in cfg.section(): print se print cfg.items(se) # print self.cfg.items(se) print '=======================' 第25行 section后面要加个s 第29行 少了个self 第49行 少了一个等于号:if __name__ =='__main__': 第58行 拼错了吧。 info.save()
Python文件处理
88454 学习 · 107 问题
相似问题