Text attributes from tuple

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KHATatonic
    New Member
    • Mar 2007
    • 4

    Text attributes from tuple

    Hi all - first off, I apologize in advance for being a novice. Any help would be greatly appreciated.

    I'm a graduate student in psychology trying out a new programming language for my experiments. I'm going to be using PyEPL which is based on Python. Coming from AppleScript (and having very little programming experience overall), I'm a little overwhelmed trying to get my first experiment to work. Fortunately, what I need doesn't seem like it should be all that complicated. I just need to start thinking in Python.

    Basically, I need to read in a text file, which is composed of 3 columns, the first of which contains a cue word; the second a response word, while the third column contains a dummy code letting the program know how to present the word pair (e.g., present the string in [R]ED or [G]REEN). The experiment will run through each line of text one at a time (i.e., each line is one trial).

    So the structure might look like this:
    CAT, MEOW, r
    TABLE, CHAIR, g
    SHOE, SOCK, r

    In the end, I'd want to print "CAT-MEOW" in red (specified by the 'r') and TABLE-CHAIR in green.

    I was then thinking it would be best to break up each line into a tuple, so I could say something along the lines of...:

    Code:
     
    from string import strip
    input = open('Desktop/TNTtest/input_test_comma.txt')
    data = []
    for line in input:
    	data.append (tuple (map(strip, line.split(',',1))))
    s = data[0][0] + "-" +  data[0][1]
    if data[0][2] == "r":
         print "<font color='RED'> s </FONT>"
    elif data[0][2] == "g":
          print "<font color='GREEN'> s </FONT>"
    Basically, I am wondering how to get it to set the color of the cue and response words for each tuple within data. Specifically, I'm not sure how to get it to iterate through each sub-tuple (i.e., each line) in the now combined tuple) or how to set the font color dependent on the 3rd element in each.

    About my system:

    Mac OSX 10.4, English
    Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin


    Once again, thank you in advance,

    Justin
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by KHATatonic
    Hi all - first off, I apologize in advance for being a novice. Any help would be greatly appreciated.

    I'm a graduate student in psychology trying out a new programming language for my experiments. I'm going to be using PyEPL which is based on Python. Coming from AppleScript (and having very little programming experience overall), I'm a little overwhelmed trying to get my first experiment to work. Fortunately, what I need doesn't seem like it should be all that complicated. I just need to start thinking in Python.

    Basically, I need to read in a text file, which is composed of 3 columns, the first of which contains a cue word; the second a response word, while the third column contains a dummy code letting the program know how to present the word pair (e.g., present the string in [R]ED or [G]REEN). The experiment will run through each line of text one at a time (i.e., each line is one trial).

    So the structure might look like this:
    CAT, MEOW, r
    TABLE, CHAIR, g
    SHOE, SOCK, r

    In the end, I'd want to print "CAT-MEOW" in red (specified by the 'r') and TABLE-CHAIR in green.

    I was then thinking it would be best to break up each line into a tuple, so I could say something along the lines of...:

    Code:
     
    from string import strip
    input = open('Desktop/TNTtest/input_test_comma.txt')
    data = []
    for line in input:
    	data.append (tuple (map(strip, line.split(',',1))))
    s = data[0][0] + "-" +  data[0][1]
    if data[0][2] == "r":
         print "<font color='RED'> s </FONT>"
    elif data[0][2] == "g":
          print "<font color='GREEN'> s </FONT>"
    Basically, I am wondering how to get it to set the color of the cue and response words for each tuple within data. Specifically, I'm not sure how to get it to iterate through each sub-tuple (i.e., each line) in the now combined tuple) or how to set the font color dependent on the 3rd element in each.

    About my system:

    Mac OSX 10.4, English
    Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
    [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin


    Once again, thank you in advance,

    Justin
    basically something like this, if i didn't misinterpret your requirements
    Code:
    for line in open("input_test_comma.txt"):
         cue,response,color = line.strip().split(", ")
         if color == "r":
                    ....
         elif color == "g":
                    .....

    Comment

    • dshimer
      Recognized Expert New Member
      • Dec 2006
      • 136

      #3
      If for some reason you did want to save it as a list for other processing, using ghostdogs basic read and parse it would simply be
      Code:
      >>> datalist=[]
      >>> for line in open("/tmp/test.txt"):
      ... 	datalist.append(line.strip().split(', '))
      ...
      Then to walk through the values
      Code:
      >>> for data in datalist:
      ... 	print data
      ... 
      ['CAT', 'MEOW', 'r']
      ['TABLE', 'CHAIR', 'g']
      ['SHOE', 'SOCK', 'r']
      It looks like you have the concept of addressing individual values. You might want to stay away from Python keywords like "input" for variable names.
      I'll also mention that there are a couple of great threads here containing suggestions for books and tutorials. My first recommendation for friends that want to learn python is one that I first saw mentioned by Ghostdog "How to Think Like a Computer Scientist: Learning with Python" though there are dozens of really good ones listed many of which I found at the local library.

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by dshimer
        ....
        . My first recommendation for friends that want to learn python is one that I first saw mentioned by Ghostdog "How to Think Like a Computer Scientist: Learning with Python" though there are dozens of really good ones listed many of which I found at the local library.
        of course, i would also recommend the official Python documentation site . many useful stuffs there. New users to Python should take the tutorial (by the creator) there, as well as browse through the library reference , etc...

        Comment

        • KHATatonic
          New Member
          • Mar 2007
          • 4

          #5
          Thanks to the both of you - the specific suggestions, as well as your more general advice on how to go about diving into Python were greatly welcomed.

          Cheers,

          Justin

          Comment

          Working...