python如何读取中文文件-python在window下读写文件中的中文

# 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] ' % DEFAULTSECT)

for (key, value) in self._defaults.items():

fp.write('%s = %s ' % (key, self.strValue(value).replace(' ', ' ')))

fp.write(' ')

for section in self._sections:

fp.write('[%s] ' % section)

for (key, value) in self._sections[section].items():

if key != '__name__':

fp.write('%s = %s ' %

(key, self.strValue(value).replace(' ', ' ')))

fp.write(' ')

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 erminal.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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值