在python 2.7中删除_之前和包括_的字符 [英] Remove characters before and including _ in python 2.7
本文介绍了在python 2.7中删除_之前和包括_的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码返回一个很好的可读输出.
def add_line_remove_special(ta_from,endstatus,*args,**kwargs):尝试:ta_to = ta_from.copyta(status=endstatus)infile = botslib.opendata(ta_from.filename,'r')tofile = botslib.opendata(str(ta_to.idta),'wb')开始 = infile.readline()导入文本换行行 = "\r\n".join(textwrap.wrap(start, 640))tofile.write(行)infile.close()tofile.close()
这是输出,现在我想删除所有字符,直到并包括 _
Ichg_UNBUNOA3 14 2090100000015 14 1304221445000001MSG_BGM380 610809 9 不适用MSG_DTM13720130422 102Grp1_RFFON 测试 EDIGrp2_NADBY 2090100000015 9Grp2_NADIV 2090100000015 9Grp2_NADDP 2090100000015 9Grp7_CUX2 EUR4Grp8_PAT22 5 3 D 30Grp25_LIN1 02090100000022 ENGrp25_QTY47 5Grp25_QTY12 5Grp26_MOA203 15.00Grp28_PRIINV 3000.00 1000PCEGrp33_TAX7 增值税 21.00 秒Grp25_LIN2 02090100000039 ENGrp25_QTY47 10Grp25_QTY12 10Grp26_MOA203 350.00Grp28_PRIINV 35000.00 1000PCEGrp33_TAX7 增值税 21.00 秒
我该怎么做?
解决方案
要获取下划线字符后一行的所有文本,在第一个 _
字符处拆分并取结果的最后一个元素:
line.split('_', 1)[-1]
这也适用于没有在行上有下划线字符的行.
演示:
<预><代码>>>>'Grp25_QTY47 5'.split('_', 1)[-1]'QTY47 5'>>>'无下划线'.split('_', 1)[-1]'没有下划线'将其转换为您的代码:
导入文本换行ta_to = ta_from.copyta(status=endstatus)使用 botslib.opendata(ta_from.filename,'r') 作为 infile:使用 botslib.opendata(str(ta_to.idta),'wb') 作为 tofile:对于 textwrap.wrap(next(infile), 640) 中的行:line = line.split('_', 1)[-1]tofile.write(line + '\r\n')
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(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()
This is the output, now I would like to remove all the characters until and including the _
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
How can I do this?
解决方案
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')
这篇关于在python 2.7中删除_之前和包括_的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文