V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
c00WKmdje2wZLrSI
V2EX  ›  Python

Python 替换字符串的问题

  •  
  •   c00WKmdje2wZLrSI · Sep 28, 2018 · 2099 views
    This topic created in 2771 days ago, the information mentioned may be changed or developed.
    比如这样一段字符串
    <meta name="detectify-verification" content="d0264f228155c7a1f72c3d91c17ce8fb" />
    <meta name="p:domain_verify" content="b87e3b55b409494aab88c1610b05a5f0"/>
    <meta name="alexaVerifyID" content="OFc8dmwZo7ttU4UCnDh1rKDtLlY" />
    <meta name="baidu-site-verification" content="D00WizvYyr" />

    content 里面的内容是随机的,如何把他们替换成
    <meta name="detectify-verification" content="d0264f228155c7a1f72c3d91c17ce8fb.html" />
    <meta name="p:domain_verify" content="b87e3b55b409494aab88c1610b05a5f0.html"/>
    <meta name="alexaVerifyID" content="OFc8dmwZo7ttU4UCnDh1rKDtLlY.html" />
    <meta name="baidu-site-verification" content="D00WizvYyr.html" />

    我只懂得把 content 的内容提取出来,再 for 循环一个个替换,但是感觉有点麻烦,有没有办法不使用循环,直接用 replace 或者 re 搞定?
    zhzer
        1
    zhzer  
       Sep 28, 2018 via Android
    过于基础了吧,建议百度,现在给你写了下次还要问
    imn1
        2
    imn1  
       Sep 28, 2018
    同意#1
    不就把
    " />
    替换成
    .html" />
    ?

    你不会从来没用过替换方法吧?
    luckybo
        3
    luckybo  
       Sep 28, 2018
    不就是 content=""里面的字符串+‘.html ’么😓
    c00WKmdje2wZLrSI
        4
    c00WKmdje2wZLrSI  
    OP
       Sep 28, 2018
    @zhzer
    @imn1
    不是,感觉我问的方式有问题,这种问法的确一替换就好了,我再想想怎么问好
    Raisu
        5
    Raisu  
       Sep 28, 2018
    这。。。。。。建议多做一些处理字符串的练习
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3275 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 04:51 · PVG 12:51 · LAX 21:51 · JFK 00:51
    ♥ Do have faith in what you're doing.