免费注册 查看新帖 |

Chinaunix

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

如何理解这个format函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-21 09:34 |只看该作者 |倒序浏览
本帖最后由 luofeiyu_cu 于 2014-08-21 09:35 编辑

print('My name is {0.name}'.format(open('out.txt', 'w')))

上面的语句为何输出:

My name is out.txt

这里的{0.name}  这里的0 是什么意思?
.name通过测试的方式,得到一点启示

>>> print('My name is {0.mode}'.format(open('out.txt', 'w')))
My name is w
>>> print('My name is {0.encoding}'.format(open('out.txt', 'w')))
My name is cp936
>>> print('My name is {encoding}'.format(open('out.txt', 'w')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'encoding'

0. 这个含义还是有点不清楚。

论坛徽章:
4
金牛座
日期:2013-10-11 16:12:50卯兔
日期:2014-07-31 09:17:19辰龙
日期:2014-08-08 09:28:02狮子座
日期:2014-09-14 20:32:05
2 [报告]
发表于 2014-08-21 10:03 |只看该作者
ssfjhh 发表于 2014-08-20 21:04
Fancier Output Formatting


你到底是有多无视我的帖子

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
3 [报告]
发表于 2014-08-21 13:10 |只看该作者
回复 2# ssfjhh
个人猜测,可能不是无视,有可能是没有办法读你提供的英文文档。所以,再次发贴寻求帮助。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP