Mike
Mike

Reputation: 544

Is there any way to read this file format into Python

Does this file format look like anything anyone has seen before (I trimmed a lot so you can see just the formatting):

<REAPER_PROJECT 0.1 "5.29/x64" 1493370961
  RIPPLE 0
  GROUPOVERRIDE 0 0 0
  AUTOXFADE 1
  ENVATTACH 1
  <TRACK {1157FED1-D15B-4B2B-A89E-BF3696E69030}
    NAME "Station VT"
    PEAKCOL 29129222
    <ITEM
      PLAYRATE 1 1 0 -1 0 0.0025
      CHANMODE 0
      GUID {2493C675-0CB3-433B-B8CD-D661290D7980}
      <SOURCE WAVE
        FILE "Audio\04-KTKE VT-glued-131121_1022 render 001.wav"
      >
    >
    <ITEM
      PLAYRATE 1 1 0 -1 0 0.0425
      CHANMODE 0
      GUID {2493C123-0C32-466B-B8CD-D66129123123}
      <SOURCE WAVE
        FILE "Audio\131343121_1022 render 001.wav"
      >
    >
  >
>

I've contacted the vendor of the program that generates this file to see if they could let me know how they generated the file, but I'm not sure if I'll get an answer back from them. I'm guessing it's a structure of structures or something like that? Kinda guessing as I'm not familiar with many other languages.

Ultimately I'd like to read this into Python, if possible, make some changes to the data, and then spit it back out to a file. Otherwise I guess I'll just parse it line by line and make the changes that I need - not impossible, but would be much easier if there's already a reader that exists.

EDIT

I should have mentioned that I need to output the data in the same format as I read it in, so having a reader/writer would be ideal.

Upvotes: 0

Views: 372

Answers (1)

stovfl
stovfl

Reputation: 15513

You can convert it to XML, for instance:

def to_XML():
    with open('test/REAPER_in.txt') as f_in,\
         open('test/REAPER.xml', 'w') as f_out:

        tags = []
        while True:
            line = f_in.readline()[:-1].strip(' ')
            if not line: break

            if line.startswith('<'):
                eot = line.find(' ')
                if eot > -1:
                    tag = line[1:eot]
                    data = line[eot+1:].replace('"', '')
                    f_out.write( '<%s data="%s">\n' % (tag, data))


                else:
                    tag = line[1:]
                    f_out.write('<%s>\n' % (tag) )

                tags.append(tag)

            elif line.startswith('>'):
                tag = tags.pop()
                f_out.write('</%s>\n' % tag)

            else:
                eot = line.find(' ')
                tag = line[:eot]
                data = line[eot+1:].replace('"', '')
                f_out.write( '<%s>%s</%s>\n' % (tag, data, tag) )

Output

<REAPER_PROJECT data="0.1 5.29/x64 1493370961">  
<RIPPLE>0</RIPPLE>  
<GROUPOVERRIDE>0 0 0</GROUPOVERRIDE>  
<AUTOXFADE>1</AUTOXFADE>  
<ENVATTACH>1</ENVATTACH>  
<TRACK data="{1157FED1-D15B-4B2B-A89E-BF3696E69030}">  
<NAME>Station VT</NAME>  
<PEAKCOL>29129222</PEAKCOL>  
<ITEM>  
<PLAYRATE>1 1 0 -1 0 0.0025</PLAYRATE>  
<CHANMODE>0</CHANMODE>  
<GUID>{2493C675-0CB3-433B-B8CD-D661290D7980}</GUID>  
<SOURCE data="WAVE">  
<FILE>Audio\04-KTKE VT-glued-131121_1022 render 001.wav</FILE>  
</SOURCE>  
</ITEM>  
</TRACK>
</REAPER_PROJECT>

Upvotes: 1

Related Questions