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

[ Python ]请问这段代码中 value = value.strip()这行起到什么作用呢?

  •  
  •   yyrhwy · 2019-04-23 22:17:00 +08:00 · 2102 次点击
    这是一个创建于 1800 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import re
    states = [' Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',
    'south carolina##', 'West virginia?']


    def clean_strings(strings):
    result = []
    for value in strings:
    value = value.strip()
    value = re.sub('[!#?]', '', value)
    value = value.title()
    result.append(value)
    return result


    print(clean_strings(states))

    为了使 states 中的数据整齐,所以需要清洗数据。
    其中 value = value.strip()这行代码似乎可有可无,因为即使将其注释掉,输出结果依然没有变化。
    .strip()方法的作用是什么呢?
    qinghon
        1
    qinghon  
       2019-04-24 00:04:55 +08:00 via Android
    python strip
    Google 请
    banixc
        2
    banixc  
       2019-04-24 00:05:40 +08:00
    smallpython
        3
    smallpython  
       2019-04-24 11:48:28 +08:00
    搜一下 strip ()这个函数有什么作用就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3351 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:41 · PVG 21:41 · LAX 06:41 · JFK 09:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.