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

Python 如何代替 JS 的 toString 方法?

  •  
  •   Liang · 2019-04-29 15:50:37 +08:00 · 3398 次点击
    这是一个创建于 1821 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大家好,搜索了好久找不到方法,只好提问了

    最近在做爬虫,要把一部分的 JS 转到 Python 来计算,其中有个Math.random().toString(16)语句,在 Python 中没办法转过来,我用random.random()可以得出Math.random()的数,例如0.480366642537

    但是 toString(16)无法找到代替的方法,尝试了hex()float.hex()都不行。

    JS:

    >>> (0.480366642537).toString(16)
    0.7af94eebc92618
    

    Python:

    >>> float.hex(0.480366642537)
    0x1.ebe53baf24986p-2
    

    请问各位大佬,我如何能得到一个跟 JS 一样的结果?

    5 条回复    2019-04-29 18:30:32 +08:00
    snal123
        1
    snal123  
       2019-04-29 16:44:26 +08:00 via iPhone   ❤️ 1
    pyexcejs 试试
    ipwx
        2
    ipwx  
       2019-04-29 16:54:28 +08:00
    snal123
        3
    snal123  
       2019-04-29 16:54:46 +08:00 via iPhone
    pyexecjs 打错了
    Liang
        4
    Liang  
    OP
       2019-04-29 18:19:00 +08:00
    @snal123 感谢,目前是通过 Naked 执行 JS 获得结果,原理是一样的
    @ipwx 可能因为不太熟悉原理吧,要恶补。。感谢
    coolair
        5
    coolair  
       2019-04-29 18:30:32 +08:00 via Android
    其实你那样也没错,只不过他用了科学记数法,p 后面,所以不一样。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5440 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:45 · PVG 14:45 · LAX 23:45 · JFK 02:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.