Question

I have a text file (songs.txt) containing many lines that look like the quoted text below. What I want is to take apart the left side from the right. The sides will be determined depending on which side they stand to &&&&&&. There are two parts, the search string and song length. For example:

Don Diablo - M1 stinger &&&&&& 204

The left part is the text to the right of the &&&&&&, and the right part is the part is right of the &&&&&&

Left part: Don Diablo - M1 stinger

Right part: 204

Since then these commands can be executed through a PIPE. There are two commands per line in the text file, then it is a delay at the same length as the right part (song length, it is set in seconds).

For example:

search Don Diablo - M1 stinger

play 1

Now there will be a break at the same 204 seconds. Then it goes to fetch the next line from the file. Likes and continue so all the time.

Problem is, I've played around in Python a bit and now I really have no clue. I've read about some solutions but they weren't as helpful as I though.

I would just be pleased if someone could tell me how to do it in practice?, is it even possible?

If someone would be hacking on this, here you have the songs.txt

Note: I'm using Python 2.7.4

Was it helpful?

Solution

I'm not clear what you mean by 'piped' commands, but I think there are three key elements to a solution:

  1. In Python, files are 'iterable', which means that you can use an open (text) file on the right hand side of a for loop, and it will iterate over the files' lines:

    for line in open('songs.txt'):
    
  2. Splitting the left and right side of a line can be done using the partition method of string objects:

        left, sep, right = line.partition('&&&&&&')
    

    Note that the 'right' element will contain the line ending character(s), and both left and right may contain additional whitespace. You probably want to remove all this with strip.

        left = left.strip()
        right = right.strip()
    
  3. Actually running commands should probably be done with subprocess, which can handle blocking calls (using call or check_call) or running commands in the background with pipes attached using Popen. Note that all these commands want a list of arguments rather than just a command line string - the subprocess docs should help here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top