Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a bit of trouble changing position of some words in specific lines from text file look like this:
product  name="anything" s_c="7" act_ferq="UREWUERWERU" sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common

product  name="anything" s_c="7" control_type="pos" act_ferq="UREWUERWERU" 
sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common

product  name="anything" s_c="7" act_ferq="UREWUERWERU" sys_name="mhr22:21140016-21140079"
common name="xyz" value="1234"
common

product  name="anything" s_c="7" control_type="neg" act_ferq="UREWUERWERU" 
sys_name="mhr22:21140017-21140076"
common name="xyz" value="1234"
common

I want it to look like:
product  name="anything" sys_name="mhr22:21140017-21140076 act_ferq="UREWUERWERU" s_c="7"
common name="xyz" value="1234"
common


product  name="anything" sys_name="mhr22:21140017-21140076" control_type="pos" 
common name="xyz" value="1234"
common

product  name="anything" sys_name="mhr22:21140016-21140079 act_ferq="UREWUERWERU" s_c="7"
common name="xyz" value="1234"
common


product  name="anything" sys_name="mhr22:21140017-21140076" control_type="neg"
common name="xyz" value="1234"
common


What I have tried:

I am working on it by using python regex here's what I am following (pseudocode):
filein=open('inputfile.txt','r')
fileout=open('fileout.txt','w')

for line in filein.readlines():
    if re.search('product  name="[^\s]+"',line):
        if "control_type" in line:
            delete  s_c="7" & act_ferq="UREWUERWERU"
            and rearrange line
           (#like this# product  name="anything" sys_name="mhr22:21140017-21140076" 
control_type="neg")
        else:
            rearrange line 
            print (line)
            (#like this# #product  name="anything" sys_name="mhr22:21140017-21140076 
act_ferq="UREWUERWERU" s_c="7")


Thank you in advanced!
Posted
Updated 23-Jun-21 1:58am
Comments
Richard MacCutchan 23-Jun-21 3:51am
   
It would be simpler to use the split method on the strings and then re-order the separate fields according to your requirements.

1 solution

Try this:
Python
def reformat():
    product = {}
    f = open('inputfile.txt', 'r')
    for line in f:
        line = line.rstrip('\n')
        if line.startswith('product'):
            prod = line.split()
            for field in prod[1:]:
                key, value = field.split('=')
                product[key] = value
        elif line.startswith('sys_name'):
            key, value = line.split('=')
            product[key] = value
        else:
            # write out the product fields first
            if len(product) > 0:
                print('product', end=' ')
                if 'control_type' in product:
                    for key in [ 'name', 'sys_name', 'control_type' ]:
                        print(F'{key}={product[key]}', end = ' ')
                else:
                    for key in [ 'name', 'sys_name', 'act_ferq', 's_c' ]:
                        print(F'{key}={product[key]}', end = ' ')
                print('')
                product = {}
            print(line)

This is a quick and dirty solution and can be cleaned up in a few areas. Obviously the print statements need to be changed to write to the output file.
   
Comments
Maciej Los 23-Jun-21 9:30am
   
"quick adn dirty solution" - i need to remember this.
5!
Richard MacCutchan 23-Jun-21 9:52am
   
It is an excuse for poor work. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900