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
V2EX  ›  Python

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

  •  
  •   initialdp · 2015-09-21 13:05:36 +08:00 · 4718 次点击
    这是一个创建于 3132 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python 版本是 2.7.3 。

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

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

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


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

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

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

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

    只是不太清楚为什么要这么做。
    ethego
        19
    ethego  
       2015-09-21 14:42:30 +08:00
    @initialdp 为啥不用 abs 函数。。
    TimePPT
        20
    TimePPT  
       2015-09-21 15:17:32 +08:00
    2.7 以上建议用 format
    hahastudio
        21
    hahastudio  
       2015-09-21 15:38:50 +08:00   ❤️ 1
    msg7086
        22
    msg7086  
       2015-09-21 20:17:55 +08:00
    #14 @ethego python 自己现在还是半股浓浓的面向过程风,怎么看怎么难受……
    ethego
        23
    ethego  
       2015-09-21 21:16:05 +08:00
    @msg7086 python 又不限制你强制面向对象,当然提供了很多面向过程的写法,不爽你可以全部用面向对象的写法写 python 也没有问题啊。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5349 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:32 · PVG 09:32 · LAX 18:32 · JFK 21:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.