python在window下读写文件中的中文

来源:互联网 发布:svn服务器搭建windows 编辑:程序博客网 时间:2024/06/07 11:47
 

#   Programmer: limodou
#   E-mail:    
chatme@263.net
#
#   Copyleft 2004 limodou
#
#   Distributed under the terms of the GPL (GNU Public License)
#
#   NewEdit is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   $Id: IniFile.py,v 1.2 2004/07/20 14:15:44 limodou Exp $

import os
import sys
import codecs
from ConfigParser import ConfigParser, NoOptionError, NoSectionError
import types

class IniFile(ConfigParser):
#    def __init__(self, inifile='config.ini', saveimmediately=False, encoding='utf-8'):
    def __init__(self, inifile, saveimmediately=False, encoding=None):
        ConfigParser.__init__(self)
        self.inifile=inifile
        self.saveimmediately = saveimmediately
        self.encoding = encoding
        if inifile:
            self.read(inifile)

    def read(self, inifile):
        self.inifile=inifile
        if inifile:
            try:
                fp = file(self.inifile, 'rb')
            except:
                fp = None
            if fp:
                if self.encoding:
                    reader = codecs.lookup(self.encoding)[2](fp)
                else:
                    reader = fp
                self.readfp(reader)
                reader.close()

    def get(self, sec, option, default=None):
        #"Get an option value for given section or return default
        if self.has_option(sec, option):
            return ConfigParser.get(self, sec, option, raw=0, vars=None)
        else:
            return default

    def getint(self, sec, option, default=0):
        if self.has_option(sec, option):
            return ConfigParser.getint(self, sec, option)
        else:
            return default
        
    def getfloat(self, sec, option, default=0.0):
        if self.has_option(sec, option):
            return ConfigParser.getfloat(self, sec, option)
        else:
            return default

    def getboolean(self, sec, option, default=0):
        if self.has_option(sec, option):
            return ConfigParser.getboolean(self, sec, option)
        else:
            return default

    def set(self, section, option, value):
        if not self.has_section(section):
            self.add_section(section)
        ConfigParser.set(self, section, option, value)
        if self.saveimmediately:
            self.save()

    def remove_section(self, section):
        res=ConfigParser.remove_section(self, section)
        if res and self.saveimmediately:
            self.save()
        return res
    
    def remove_option(self, section, option):
        try:
            res=ConfigParser.remove_option(self, section, option)
            if res and self.saveimmediately:
                self.save()
            return res
        except NoSectionError:
            return 0

    def add_section(self, section):
        ConfigParser.add_section(self, section)
        if self.saveimmediately:
            self.save()

    def save(self):
        fp=file(self.inifile, "wb")
        if self.encoding:
            writer = codecs.lookup(self.encoding)[3](fp)
        else:
            writer = fp
        self.write(writer)
        writer.close()
        
    def write(self, fp):
        #Write an .ini-format representation of the configuration state.
        if self._defaults:
            fp.write('[%s]\r\n' % DEFAULTSECT)
            for (key, value) in self._defaults.items():
                fp.write('%s = %s\n' % (key, self.strValue(value).replace('\n', '\n\t')))
            fp.write('\n')
        for section in self._sections:
            fp.write('[%s]\r\n' % section)
            for (key, value) in self._sections[section].items():
                if key != '__name__':
                    fp.write('%s = %s\r\n' %
                             (key, self.strValue(value).replace('\n', '\n\t')))
            fp.write('\n')

    def strValue(self, value):
        if type(value) in (types.StringType, types.UnicodeType):
            return value
        else:
            return str(value)

 以上是自己构造的一个类,使用例子如下:

#coding=utf-8

import IniFile
'''
ini=IniFile.IniFile('test.ini', encoding='utf-8')

ini.set('中文测试', '缺省', '中国')

ini.save()
'''
ini=IniFile.IniFile(r'data\terminal.ini', encoding='utf-8')
#ini.set('中文测试', '缺省', '中国')

ini.set('config', 'terminal_name', unicode('测试', 'utf8'))
ini.set('config', 'terminal_desc', unicode('描述', 'utf8'))
ini.set('config', 'server_ip', '192.168.121.20')
ini.set('config', 'mode', '0')
ini.set('config', 'start', '0')
ini.set('config', 'terminal_id', '2-33-715654220')
ini.save()

#print ini.get('config', 'terminal_name')