免费注册 查看新帖 |

Chinaunix

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

我发现ConfigParser模块的打印没有顺序可言!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-06 11:23 |只看该作者 |倒序浏览
取自python 标准库附带示例代码,并且做了一些修改:

  1. import ConfigParser
  2. import sys
  3. config = ConfigParser.ConfigParser()# set a number of parameters
  4. config.add_section("book")
  5. config.set("book", "title", "the python standard library")
  6. config.set("book", "author", "fredrik lundh")

  7. config.add_section("ematter")
  8. config.set("ematter", "pages", 250)# write to screen
  9. config.add_section("ematter123")
  10. config.set("ematter123", "pages123", 250)# write to screen

  11. config.write(sys.stdout)
  12.                  
复制代码


打印的顺序是:

  1. [book]
  2. author = fredrik lundh
  3. title = the python standard library

  4. [ematter123]
  5. pages123 = 250

  6. [ematter]
  7. pages = 250
复制代码


但是在Python自带的手册中,却是这么说的:

  1. # When adding sections or items, add them in the reverse order of
  2. # how you want them to be displayed in the actual file.
  3. # In addition, please note that using RawConfigParser's and the raw
  4. # mode of ConfigParser's respective set functions, you can assign
  5. # non-string values to keys internally, but will receive an error
  6. # when attempting to write to a file or when you get it in non-raw
  7. # mode. SafeConfigParser does not allow such assignments to take place.

复制代码


也就是说,打印的时候会按照添加的逆序进行打印,但是上面的结果发现根本不是这么回事.

论坛徽章:
0
2 [报告]
发表于 2009-07-06 11:54 |只看该作者
按字母顺序?

论坛徽章:
0
3 [报告]
发表于 2009-08-29 08:50 |只看该作者
这可是个known problem了,python 3.1之前的都应该有这个问题。因为ConfigParser的核心是一个dictionary,而dictionary在取出key的时候顺序是不确定的。请看这里 http://docs.python.org/3.1/whats ... rdered-dictionaries

论坛徽章:
0
4 [报告]
发表于 2009-11-21 16:39 |只看该作者
不是要它有顺序,但应该也应该是按插入的顺序吧?貌似也不是这样的。不知有否解决方法?

论坛徽章:
0
5 [报告]
发表于 2009-11-22 09:40 |只看该作者
ConfigParser很烂。自己写一个都比他强。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP