Home > Net >  Find Remove all characters after :202::TEST// and :200::DATE1//
Find Remove all characters after :202::TEST// and :200::DATE1//

Time:02-01

** This is a old.txt file:

:100:ABCD :120:HELP :202:TEST//1234567 :203:PROD// :230:NEWBEE :240:DDDD// :250:FFFF// :200::DATE1//01-01-2019 :300::DATE2// :100:ABCD

The new file.txt file should look like

:100:ABCD :120:HELP :202:TEST// :203:PROD// :230:NEWBEE :240:DDDD// :250:FFFF// :200::DATE1// :300::DATE2// :100:ABCD

***This is what I have now but this will not remove

import os
texttofind= ':202::TEST//'
texttoreplace= ':202::TEST//{{TEST}}'
sourcepath = os.listdir('/Users/Testin/')
for file in sourcepath:
    inputfile = '/Users/Testin/'   file
    print ('Convert is now ongoing :'  inputfile)
    with open (inputfile, 'r') as inputfile:
        filedata = inputfile.read()
        freq= 0
        freq = filedata.count(texttofind)
    destiationpath= '/Users/Testout/'   file
    filedata = filedata.replace(texttofind,texttoreplace)
    with open(destiationpath,'w') as file:
        file.write(filedata)
    print ('Total %d Records Replaced' %freq)

** The result is this...(but I need the number 1234567(or any other number or character, to be removed before it can be replaced)

:10A:ABCD
:120:HELP
:202::TEST//{{TEST}}1234567
:203::PROD//
:230:NEWBEE
:240::DDDD//EEEE
:250::FFFF//FFFF
:20::DATE1//01012019
:30::DATE2//
:10B:ABCD
 -}

CodePudding user response:

I can help you, just watch this video: https://youtu.be/857FiLpQtxk

CodePudding user response:

You probably don't need pandas, and even not python for that.

You could use sed:

sed 's|//[^ /]*|//|g' old.txt >new.txt
  •  Tags:  
  • Related