免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 19519 | 回复: 31
打印 上一主题 下一主题

每周一题——读写配置文件。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-20 16:56 |只看该作者 |正序浏览
群里面的朋友提到是否应该出点题目给新加入python的朋友。我想值得一试。

本周题目如下:
读写配置文件是经常碰到操作,请大家写出自己读写配置文件的程序,要求:
1、配置文件可以以#作为注释,并不处理。
2、所写的程序应以import导入使用。
3、有自测试代码。
4、有注释。

希望大家踊跃参加。

论坛徽章:
0
32 [报告]
发表于 2008-04-14 23:38 |只看该作者

回复 #1 xichen 的帖子

我也是新手,而且是完全没基础的新手

非常不好意思的问一下,什么是配置文件?

论坛徽章:
0
31 [报告]
发表于 2008-04-11 00:16 |只看该作者
本来还想写一个,一看大家都是05年写的,惭愧惭愧,还是算了

论坛徽章:
0
30 [报告]
发表于 2008-04-10 17:50 |只看该作者
Py比xml简单,凭什么要用xml做配置文件?

这个题目很不错的。如果是标准答案,是不是我也贡献一个?
yikalu 该用户已被删除
29 [报告]
发表于 2008-04-08 12:29 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
28 [报告]
发表于 2005-12-07 11:38 |只看该作者
就用dict4ini了

论坛徽章:
0
27 [报告]
发表于 2005-11-29 13:25 |只看该作者
dict4ini

http://wiki.woodpecker.org.cn/moin/Dict4Ini

一个把ini当成字典使用的ini处理模块

论坛徽章:
0
26 [报告]
发表于 2005-11-29 13:14 |只看该作者
也来凑个热闹
支持默认值和多行属性


  1. '''
  2. The <code>Properties</code> class represents a persistent set of
  3. properties. The <code>Properties</code> can be saved to a stream
  4. or loaded from a stream. Each key and its corresponding value in
  5. the property list is a string.
  6. <p>
  7. A property list can contain another property list as its
  8. "defaults"; this second property list is searched if
  9. the property key is not found in the original property list.
  10. </p>
  11. '''

  12. class Properties:
  13.     def __init__(self, prop={}, default={}):
  14.         self.prop = prop
  15.         self.default = default

  16.     def put(self, key, value):
  17.         self.prop[key] = value

  18.     def get(self, key, default=None):
  19.         return self.prop.get(key, self.default.get(key, default))

  20.     def load(self, filename):
  21.         import os
  22.         if os.path.exists(filename) and os.path.isfile(filename):
  23.             fd = open(filename, 'r')
  24.             lines = fd.readlines()
  25.             fd.close()
  26.             keyvalue = ''
  27.             continueLine = False
  28.             for i in range(len(lines)):
  29.                 line = lines[i].strip()
  30.                 if line[0] == '#':
  31.                     continue
  32.                 if continueLine:
  33.                     if line[-1:] == '\\':
  34.                         line = line[:-1]
  35.                         keyvalue += line
  36.                         continue
  37.                     else:
  38.                         keyvalue += line
  39.                         continueLine = False
  40.                 else:
  41.                     if line[-1:] == '\\':
  42.                         keyvalue = line[:-1]
  43.                         continueLine = True
  44.                         continue
  45.                     else:
  46.                         keyvalue = line
  47.                         continueLine = False
  48.                 if keyvalue.find('=') < 0:
  49.                     continue
  50.                 key, value = keyvalue.split('=', 1)
  51.                 self.prop[key.strip()] = value.strip()

  52. if __name__ == '__main__':
  53.     p = Properties({'propname':'propname'}, {'defaultname':'defaultname'})
  54.     p.load('test.properties')
  55.     print p.get('noname'), p.get('noname', 'default-name')
  56.     print p.get('name'), p.get('name', 'default-name')
  57.     print p.get('propname'), p.get('propname', 'default-name')
  58.     print p.get('defaultname'), p.get('defaultname', 'default-name')
  59.     print p.get('value'), p.get('value', 'default-value')

复制代码


用于测试的test.properties文件内容
  1. # Properties test
  2. name = properties name
  3. value = multi-\
  4.         line \
  5.         value \
  6.         properties
  7. dirty lines
  8. # noname = end lines
复制代码

[ 本帖最后由 jkit 于 2005-11-29 14:35 编辑 ]

论坛徽章:
0
25 [报告]
发表于 2005-11-28 21:24 |只看该作者
不同的问题需要不同的方法。有些东西使用xml文件,读起来麻烦,写起来麻烦,用起来麻烦。够用就是好的。

论坛徽章:
0
24 [报告]
发表于 2005-11-28 21:01 |只看该作者
不是说写自己的配置文件不好,建议用XML来编写配置文件,这样通用性更强些!
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP