Remove characters before and including _ in python 2.7

前端 未结 1 401
北荒
北荒 2021-02-03 19:35

The following code returns into a nice readable output.

def add_line_remove_special(ta_from,endstatus,*args,**kwargs):
    try:
        ta_to = ta_from.copyta(st         


        
1条回答
  •  误落风尘
    2021-02-03 20:11

    To get all text on a line after a underscore character, split on the first _ character and take the last element of the result:

    line.split('_', 1)[-1]
    

    This will also work for lines that do not have an underscore character on the line.

    Demo:

    >>> 'Grp25_QTY47               5'.split('_', 1)[-1]
    'QTY47               5'
    >>> 'No underscore'.split('_', 1)[-1]
    'No underscore'
    

    Translating this to your code:

    import textwrap
    
    ta_to = ta_from.copyta(status=endstatus)
    with botslib.opendata(ta_from.filename,'r') as infile:
        with botslib.opendata(str(ta_to.idta),'wb') as tofile:
            for line in textwrap.wrap(next(infile), 640):
                line = line.split('_', 1)[-1]
                tofile.write(line + '\r\n')
    

    0 讨论(0)
提交回复
热议问题