【问题标题】:Remove characters before and including _ in python 2.7在 python 2.7 中删除之前和包括 _ 的字符
【发布时间】:2013-04-30 14:42:53
【问题描述】:

以下代码返回一个可读的输出。

def add_line_remove_special(ta_from,endstatus,*args,**kwargs):
    try:
        ta_to = ta_from.copyta(status=endstatus)
        infile = botslib.opendata(ta_from.filename,'r')
        tofile = botslib.opendata(str(ta_to.idta),'wb')
        start = infile.readline()
        import textwrap
        lines= "\r\n".join(textwrap.wrap(start, 640))
        tofile.write(lines)
        infile.close()
        tofile.close()

这是输出,现在我想删除所有字符,直到并包括 _

Ichg_UNBUNOA3                                   14                2090100000015                      14                1304221445000001
MSG_BGM380                                         610809                             9  NA
MSG_DTM13720130422                           102
Grp1_RFFON test EDI
Grp2_NADBY 2090100000015                         9
Grp2_NADIV 2090100000015                         9
Grp2_NADDP 2090100000015                         9
Grp7_CUX2  EUR4
Grp8_PAT22                                                                                              5  3  D   30
Grp25_LIN1        02090100000022                     EN
Grp25_QTY47               5
Grp25_QTY12               5
Grp26_MOA203             15.00
Grp28_PRIINV        3000.00           1000PCE
Grp33_TAX7  VAT                                                                                 21.00                              S
Grp25_LIN2        02090100000039                     EN
Grp25_QTY47              10
Grp25_QTY12              10
Grp26_MOA203            350.00
Grp28_PRIINV       35000.00           1000PCE
Grp33_TAX7  VAT                                                                                 21.00                              S

我该怎么做?

【问题讨论】:

    标签: python string python-2.7 split word-wrap


    【解决方案1】:

    要获取下划线字符后的所有文本,请在第一个 _ 字符处拆分并获取结果的最后一个元素:

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

    这也适用于没有行有下划线字符的行。

    演示:

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

    将其转换为您的代码:

    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')
    

    【讨论】:

    • 我需要在行中添加 for line: line.split('_',1)[-1] between lines = .... and tofile.write...
    • 是的,您需要对每一行都这样做。
    • infile = botslib.opendata(ta_from.filename,'r') tofile = botslib.opendata(str(ta_to.idta),'wb') start = infile.readline() import textwrap lines= "\r\n".join(textwrap.wrap(start, 640)) for line in lines: line.split('_', 1)[-1] tofile.write(lines) infile.close() tofile.close() 仅导致文件的最后一个字符 = S
    • 你没有对line.split的结果做任何事情。您需要存储或直接写入:例如for line in lines: tofile.write(line.split('_', 1)[-1])
    • infile = botslib.opendata(ta_from.filename,'r') tofile = botslib.opendata(str(ta_to.idta),'wb') start = infile.readline() import textwrap lines= "\r\n".join(textwrap.wrap(start, 640)) for line in lines: tofile.write(line.split('_', 1)[-1]) infile.close() tofile.close() 导致输出包含 _ 之前的字符
    猜你喜欢
    • 2018-11-16
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多