Results 1 to 3 of 3

Thread: Help with Python strings

  1. #1
    Join Date
    May 2007
    Location
    Philadelphia
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    [Solved] Help with Python strings

    I don't have any experience at all in Python, so please excuse me if I'm going about this the totally wrong way, but what I need to do is find the current volume of Alsa's master channel. Here's what I have so far:

    Code:
     f = os.popen ("amixer -c 0 set Master 0dB+")
    
        counter = 0
        
        for i in f.readlines ():
            counter += 1
            if counter == 5:
              vol_line = i
        
        start_s = vol_line.find ("[", 0, 50)
        end_s = vol_line.find ("%", 0, 50)
        
        start_i = int (start_s)
        end_i = int (end_s)
        real_vol = vol_line [start_i, end_i]
        print real_vol
    And I get the error

    Code:
    File "vol.py", line 117, in main
        real_vol = vol_line [start_i, end_i]
    TypeError: string indices must be integers, not tuple
    So I have no idea what I'm doing wrong... as far as I can see, I convert the tuple start_s and end_s into an int with the int () function, but I guess that's not happening?

    If you can please help, thanks!
    Last edited by Yes; October 20th, 2010 at 01:46 AM.

  2. #2
    Join Date
    Mar 2005
    Beans
    947
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Help with Python strings

    The tuple is the new one you created, right there in that line that's quoted in the error message. Assuming you're going for a substring there, the correct syntax would use a colon, not a comma:

    real_vol = vol_line [start_i:end_i]

    Also, your int() lines are unneeded -- start_s and end_s are already ints.

  3. #3
    Join Date
    May 2007
    Location
    Philadelphia
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Help with Python strings

    Oh, that makes way more sense. I thought it was odd find () didn't just give me an int, thanks very much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •