python删除字符并将crlf添加到fi

2024-06-16 14:54:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下文件

Ichg_UNBUNOA3                                   14                2090100000015                      14                1304221445000001                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         MSG_BGM380                                         610809                             9  NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     MSG_DTM13720130422                           102                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Grp1_RFFON test EDI                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Grp2_NADBY

我需要使用python 2.7处理它,并在每640个字符后添加一个\r\n。 这将导致

^{pr2}$

然后删除''uu'之前的所有字符

有人能解决这个问题吗?在


    import textwrap
    original= infile.readline()

    line="\r\n".join(textwrap.wrap(original, 640))
    for line in line:
        tofile.write(line)

此代码将导致以下结果

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

但现在我想把第一个字符去掉直到''uu'


Tags: testlinemsg字符nauuedigrp2
2条回答

您可以使用^{}模块:

>>> import textwrap
>>> strs="Ichg_UNBUNOA3                                   14                2090100000015                      14                1304221445000001                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         MSG_BGM380                                         610809                             9  NA                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     MSG_DTM13720130422                           102                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                Grp1_RFFON test EDI                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             Grp2_NADBY"

#textwrap.fill(strs,640) appends a newline ("\n") after every 640 characters
#use "\r\n".join(textwrap.wrap(strs, 640)) if you want '\r\n' instead of '\n' as newline 

>>> new_strs=textwrap.fill(strs,640)

>>> for line in new_strs.splitlines():
    print " ".join(line.split())
...     
Ichg_UNBUNOA3 14 2090100000015 14 1304221445000001
MSG_BGM380 610809 9 NA
MSG_DTM13720130422 102
Grp1_RFFON test EDI
Grp2_NADBY

要删除第一个字符直到_,可以在_上拆分字符串并只选择第二部分。在

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

相关问题 更多 >