Difficulty parsing text file Python 2.7
Question: Using Python 2.7, I want to take a file as input, remove some charachters from
it, and write that to another file. I'm not entirely succeeding with the below
code:

    
    
    print 'processing .ujc file for transmit'
    infile, outfile = open('app_code.ujc','r'), open('app_code_transmit.ujc','w')
    data = infile.read()
    data = data.replace("#include <avr/pgmspace.h> const unsigned char uj_code[] PROGMEM = {", "")
    data = data.replace("0x", "")
    data = data.replace(", ", "")
    data = data.replace("};", "")
    outfile.write(data)
    

The input file (example) is:

    
    
    #include <avr/pgmspace.h>
    
    const unsigned char uj_code[] PROGMEM = {
    
        0x00, 0x03, 0xB1, 0x4B, 0xEC, 0x00, 0x1D, 0x00, 0x1E, 0x00, 0x21, 0x00, 0x02, 0x6A, 0x00, 0x02, 
        0x6A, 0x00, 0x02, 0xE3, 0x3F, 0x00, 0x1F, 0x00, 0x02, 0x2C, 0x00, 0x01, 0x3B, 0x00, 0x02, 0x36, 0x00, 0x00
    };
    

And this should become (the etc is a continuation of the above and not
actually present):

    
    
    0003B14BEC001D001E002100026A00(...etc...)02360000
    

What I get with the above code is:

    
    
    #include <avr/pgmspace.h>
    
    const unsigned char uj_code[] PROGMEM = {
    
        0003B14BEC001D001E002100026A00(...etc...)
        02360000
    

In other words, I want to remove all character, empty lines, and 0x and stuff
except the actual bytes in a single continuous line but I'm tripping a little
bit on the nuances, I'm expecting. Any help?


Answer: @MKesper is right. When you read the file, there are \n or \r\n (line
separators) depending on your OS. Looking at the expected output, I feel the
better way would be to extract the data you need rather than delete the
unwanted data. I would take some help from regular expression and here is my
attempt:

    
    
    import re
    print 'processing .ujc file for transmit'
    infile, outfile = open('app_code.ujc','r'), open('app_code_transmit.ujc','w')
    data = infile.read()
    # Expect 0003B14BEC001D001E002100026A00026A0002E33F001F00022C00013B0002360000 to be the output
    outfile.write(''.join(re.findall('0x([0-9a-fA-F][0-9a-fA-F])', data)))
    

Update 1: This is based on assumption that you do not have any other 0x. Else
we need to update our regular expression

