推荐学习书目
› 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
bestehen
V2EX  ›  Python

有个字符串 有很多个逗号隔开的,但是我想去除最后一个逗号,如何解决

  •  
  •   bestehen · Jul 30, 2018 · 10433 views
    This topic created in 3030 days ago, the information mentioned may be changed or developed.
    s=‘ a,b,c,d,’变成‘ a,b,c,d ’
    26 replies  •  2018-08-03 14:25:23 +08:00
    xubeiyan
    xubeiyan
    Jul 30, 2018❤️1
    1
    s=s[:-1]
    用 rstrip(',')也行
    bestehen
    bestehen
    OP
    Jul 30, 2018
    2
    @xubeiyan 是字符串 ,你的第一个是列表吧,我说的是字符串
    ballshapesdsd
    ballshapesdsd
    Jul 30, 2018
    3
    @bestehen #2 字符串也可以这样用啊。。你这也太萌新了吧
    xubeiyan
    xubeiyan
    Jul 30, 2018❤️1
    4
    >>> s = 'a,b,c,d,'
    >>> s[:-1]
    'a,b,c,d'
    >>> s.rstrip(',')
    'a,b,c,d'
    >>>
    samv2
    samv2
    Jul 30, 2018 via Android
    5
    正则替换 (不会 python )

    ,([^,]*) $ ----> $1

    上面是 java 语法,$1 表示引用第一对括号里的值
    samv2
    samv2
    Jul 30, 2018 via Android
    6
    正则替换 (不会 python )

    ,([^,]*) $ ----> $1

    上面是 java 正则语法,$1 表示引用第一对括号里的值
    GlobalNPC
    GlobalNPC
    Jul 30, 2018
    7
    @bestehen 一楼正解
    tan90
    tan90
    Jul 30, 2018
    8
    $a="12313,1,123,1,";
    $a_nu = strlen($a);
    if (substr($a,$a_nu-1,$a_nu)==","){
    $a=substr($a,0,$a_nu-1);
    }
    php,临时想的,可以过审吗?
    VDimos
    VDimos
    Jul 30, 2018 via Android
    9
    split 后更改列表再 join 呗
    ry_wang
    ry_wang
    Jul 30, 2018
    10
    awk -F',' '{ print $NF }'
    ry_wang
    ry_wang
    Jul 30, 2018
    11
    - -! 理解错楼主的需求了。。。忽略楼上
    tan90
    tan90
    Jul 30, 2018
    12
    s='a,b,c,d,'
    s_nu=len(s)
    if s[s_nu-1:s_nu]==',':
    s=s[0:s_nu-1]
    长度,判断,重新赋值
    我测试过,可以跑的通
    input2output
    input2output
    Jul 30, 2018
    13
    str := "a,b,c,d,"
    str = str[:len(s)-1]
    frmongo
    frmongo
    Jul 30, 2018
    14
    如下

    a="this,is,that,is,what"
    b=a.split(',')
    b.pop()
    c=','.join(b)
    liaohongxing
    liaohongxing
    Jul 30, 2018
    15
    $newString = rtrim(‘ a,b,c,d,’, ‘,’);
    liaohongxing
    liaohongxing
    Jul 30, 2018
    16
    php
    lhx2008
    lhx2008
    Jul 30, 2018
    17
    @frmongo 为啥要 pop 一个
    jiqing
    jiqing
    Jul 30, 2018
    18
    java 我还想了半天怎么实现,直接有个方法
    public static void main(String[] args) {

    String s="a,b,c,d,";
    System.out.println(s.substring(0, s.length()-1));

    }
    mec
    mec
    Jul 30, 2018
    19
    [:-1]
    princelai
    princelai
    Jul 31, 2018 via Android
    20
    一楼就是正解啊,py 里可迭代对象都能切片操作
    yxcoder
    yxcoder
    Jul 31, 2018
    21
    字符串去掉最后一个字符吧
    TonyLiu2ca
    TonyLiu2ca
    Jul 31, 2018
    22
    题主题目所述与题内所给示例有出入,造成歧义

    按照题目的意思是:去除最后一个逗号。那么字符串可能是:a,b,c,d, 也可能是 a,b,c,d,ef
    也就是说最后一个逗号后面可能有也可能没有其它非逗号字符(#14 楼考虑了这种情况)

    题主没有给出如果有其它字符,去除最后一个逗号后,其它字符如何处理。有可能是全部删除,也有可能是只删除逗号字符本身。

    所以此题,暂时无解
    lixinyu1024
    lixinyu1024
    Jul 31, 2018
    23
    python 的话
    str.strip(',')
    frmongo
    frmongo
    Jul 31, 2018
    24
    @lhx2008 最后一个逗号后的丢弃
    daemonghost
    daemonghost
    Jul 31, 2018
    25
    bash
    s=${s::-1}
    houzhiqiang
    houzhiqiang
    Aug 3, 2018
    26
    a = "13asda,asda,"
    if a.endswith(","):
    a = a[:-1]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   939 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 22:42 · PVG 06:42 · LAX 14:42 · JFK 17:42
    ♥ Do have faith in what you're doing.