Search code examples
pythonxmlfilefile-formatformats

What is format of this file?


I have a file like this:

+----[ Stream 0 ]
|
| Type: Video
| Codec: H264 - MPEG-4 AVC (part 10) (h264)
| Original ID: 1030
|
+----[ IRIB TV3 [Program 103] ]
|
| Status: Running
| Type: Digital television service
| Now Playing: میان برنامه
| Publisher: IRIB
|
+----[ EPG IRIB TV3 [Program 103] ]
|
| 2014-08-03 14:42:00:Hello
|
+----[ Stream 1 ]
|
| Type: Audio
| Codec: MPEG AAC Audio (mp4a)
| Original ID: 1031
|
+----[ end of stream info ]

What is format of this file , Is there any simple way to convert it to XML?


Solution

  • The language you use for parsing this file is up to you, and depends what you are familiar with. Many people would use Perl, but I would use XSLT 2.0.

    I would start by doing it line-by-line: there's a small number of different patterns to the lines, and I would translate

    +----[ Stream 0 ]
    

    to

    <Stream>0</Stream>
    

    and

    Status: Running
    

    to

    <Status>Running</Status>
    

    That would give you something that's XML, and phase 2 is to convert it into useful XML, which you can do using the grouping facilites in XSLT 2.0 to create something like

    <Report>
      <Stream nr="0">
        <Type>Video</Type>
        ...
        <Program nr="103">
          <Status>Running</Status>
          ...
        </Program>
      </Stream>
      <Stream>...
    </Report>
    

    But there's no magic bullet here; unless someone has already produced code you can re-use, you're going to have to write a parser for this stuff.