请教一个关于 Python repr 方法的问题 - 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
barantt01
V2EX    Python

请教一个关于 Python repr 方法的问题

  •  
  •   barantt01 268 天前 1618 次点击
    这是一个创建于 268 天前的主题,其中的信息可能已经有所发展或是发生改变。

    示例代码如下

    tmp = '{"{\"username\":\"juheso\",\"is_cache\":1}":3}' print(tmp) print(repr(tmp)) tmp_1 = r'{"{\"username\":\"juheso\",\"is_cache\":1}":3}' print(tmp_1) 

    我运行之后的结果为

    {"{"username":"juheso","is_cache":1}":3} '{"{"username":"juheso","is_cache":1}":3}' {"{\"username\":\"juheso\",\"is_cache\":1}":3} 

    我想要的结果其实是{"{\"username\":\"juheso\",\"is_cache\":1}":3}

    但是为什么 repr() 没有效果呢?

    我的 python 版本为 3.10

    8 条回复    2025-01-16 22:23:36 +08:00
    superrichman
        1
    superrichman  
       268 天前
    仔细琢磨

    print('\"' == '"')
    print(r'\"' == '\\"')
    barantt01
        2
    barantt01  
    OP
       268 天前
    @superrichman 这两个都是 True 我知道,我的疑问是在 repr 方法上。因为在实际使用是这个 tmp 其实是一个变量,没法用 r''来表示
    eccstartup
        3
    eccstartup  
       268 天前
    ```python
    import json

    s = '{"{\"username\":\"juheso\",\"is_cache\":1}":3}'
    raw_s = json.dumps(s)[1:-1]
    print(raw_s) # 输出: "Hello\\nWorld"
    ```

    输出
    `{\"{\"username\":\"juheso\",\"is_cache\":1}\":3}`

    比较接近了
    julyclyde
        4
    julyclyde  
       268 天前
    你这个不是 repr 方法而是 repr 函数
    ma46
        5
    ma46  
       267 天前
    @julyclyde 内置函数 repr 调用的就是对象的__repr__方法
    ma46
        6
    ma46  
       267 天前
    对于 tmp 变量, 其内存里存的字符串就是{"{"username":"juheso","is_cache":1}":3}, 你还想 repr 输出什么东西来? 变量赋值里的 \ 根本就不是字符串的一部分, 它的作用是告诉编译器 " 是字符串的一部分

    我觉得你还没理解转义字符
    julyclyde
        7
    julyclyde  
       267 天前
    @ma46 我知道啊。但是提问是方法而非函数这个错误依然是存在的
    lijiachang
        8
    lijiachang  
       266 天前
    tmp = r'{"{\"username\":\"juheso\",\"is_cache\":1}":3}'
    print(tmp)

    声明的时候就要加 r ,否则你就写个转换函数
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3544 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 04:16 PVG 12:16 LAX 21:16 JFK 00:16
    Do have faith in what you're doing.
    ubao 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