python 格式化输出%u 的疑问? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
initialdp

python 格式化输出%u 的疑问?

  •  
  •   initialdp 2015 年 9 月 21 日 5402 次点击
    这是一个创建于 3870 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python 版本是 2.7.3 。

    请教:将一个负数按%u 格式化输出,怎么还是一个负数呢?不应该是一个无符号整数么?

    语句很简单:
    print "a=%u"%-1

    谢谢。
    23 条回复    2015-09-21 21:16:05 +08:00
    ethego
        1
    ethego  
       2015 年 9 月 21 日
    %u 的这种格式化字符串的方法非常的不 pythonic ,建议使用`.format ()`方法
    hhrmatata
        2
    hhrmatata  
       2015 年 9 月 21 日
    用-1 去替换%u ,所以当然还是负数
    ethego
        3
    ethego  
       2015 年 9 月 21 日
    我的朋友里写惯 c 系语言的人转学 python 都习惯用百分号格式化字符串。。
    initialdp
        4
    initialdp  
    OP
       2015 年 9 月 21 日
    @ethego 请问如此飘逸的方法,该怎么做呢? 谢谢。


    @hhrmatata 数字本身当然还是-1 ,但是格式化输出的结果希望是显示: a=4294967295 。 这是 C 程序的结果。
    ethego
        5
    ethego  
       2015 年 9 月 21 日
    ```python
    print "a={0}".format (abs (-1 ))
    ```
    ethego
        6
    ethego  
       2015 年 9 月 21 日
    @initialdp 字符串本身就是一个对象,有方法 format (),然后实用 abs 函数取绝对值。这样更符合 python 的风格。
    initialdp
        7
    initialdp  
    OP
       2015 年 9 月 21 日
    @ethego 居然用上了 abs 函数,这种处理方式也不好看啊。

    我改成这样也可以了:
    print "a=%u" % (-1&0xFFFFFFFF )

    总之,我以为%u 是与 C 语言对应的格式化,没想到是个坑。
    ethego
        8
    ethego  
       2015 年 9 月 21 日   1
    @initialdp 写惯 c 系的人真要改变下审美。。面向对象就是这么写的,我看起来就觉得很优雅。
    timonwong
        9
    timonwong  
       2015 年 9 月 21 日   1
    @initialdp Python 的 percent format 是没有指定 'u' 的。
    u 在 python 源码里,都是处理为 'd'
    initialdp
        10
    initialdp  
    OP
       2015 年 9 月 21 日
    @timonwong 这有什么典故么?感觉 python 将%u 处理为%d 完全多此一举,是挖坑啊。
    timonwong
        11
    timonwong  
       2015 年9 月 21 日
    @ethego 但是 logging 默认又是用的 percent format :doge:
    ethego
        12
    ethego  
       2015 年 9 月 21 日
    @timonwong 在 python2 里,数字就三种类型,长整型,整型和浮点型, python3 里就只有两种,没有了长整型。真心建议楼主放下一些 c 系的思维去学 python ,去感受一下动态类型语言的好处。
    ethego
        13
    ethego  
       2015 年 9 月 21 日
    @initialdp 这怎么叫挖坑?明明只有三种类型更加优雅怎么叫挖坑呢?
    ethego
        14
    ethego  
       2015 年 9 月 21 日
    @timonwong 感觉像是历史遗留问题,我还是全力推荐面向对象的写法。我有朋友写 c 系的转写 python 一眼就能看出来,一股浓浓的、别扭的 c 系风,怎么看怎么难受。
    timonwong
        15
    timonwong  
       2015 年 9 月 21 日
    @ethego numercial types 还有一个 complex, 还有一个 bool
    timonwong
        16
    timonwong  
       2015 年 9 月 21 日
    @ethego logging ,怎么样都是浓浓的 C-style
    dig 一下也许能发现 logging.Formatter
    ethego
        17
    ethego  
       2015 年 9 月 21 日
    @timonwong 看了下 python3 的 logging 模块,已经是面向对象的写法了,所以看起来还是个历史遗留问题。
    initialdp
        18
    initialdp  
    OP
       2015 年 9 月 21 日
    @timonwong
    @ethego

    挖了一下 python2.7 的代码,的确将 u 转成 d 处理了:
    ( 1 ) PyString_Format 函数中,'iduoxX'都按照 PyInt 处理;
    ( 2 )接着在 formatint 函数中,非常冷酷地进行转换:
    if (x < 0 && type == 'u') {
    type = 'd';
    }

    只是不太清楚为什么要这么做。
    ethego
        19
    ethego  
       2015 年 9 月 21 日
    @initialdp 为啥不用 abs 函数。。
    TimePPT
        20
    TimePPT  
    PRO
       2015 年 9 月 21 日
    2.7 以上建议用 format
    hahastudio
        21
    hahastudio  
       2015 年 9 月 21 日   1
    msg7086
        22
    msg7086  
       2015 年 9 月 21 日
    #14 @ethego python 自己现在还是半股浓浓的面向过程风,怎么看怎么难受……
    ethego
        23
    ethego  
       2015 年 9 月 21 日
    @msg7086 python 又不限制你强制面向对象,当然提供了很多面向过程的写法,不爽你可以全部用面向对象的写法写 python 也没有问题啊。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2746 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 40ms UTC 10:07 PVG 18:07 LAX 03:07 JFK 06:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86