473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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...:

Expand|Select|Wrap|Line Numbers
  1.  
  2. from string import strip
  3. input = open('Desktop/TNTtest/input_test_comma.txt')
  4. data = []
  5. for line in input:
  6.     data.append (tuple (map(strip, line.split(',',1))))
  7. s = data[0][0] + "-" +  data[0][1]
  8. if data[0][2] == "r":
  9.      print "<font color='RED'> s </FONT>"
  10. elif data[0][2] == "g":
  11.       print "<font color='GREEN'> s </FONT>"
  12.  
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
Mar 31 '07 #1
4 1667
ghostdog74
511 Expert 256MB
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...:

Expand|Select|Wrap|Line Numbers
  1.  
  2. from string import strip
  3. input = open('Desktop/TNTtest/input_test_comma.txt')
  4. data = []
  5. for line in input:
  6.     data.append (tuple (map(strip, line.split(',',1))))
  7. s = data[0][0] + "-" +  data[0][1]
  8. if data[0][2] == "r":
  9.      print "<font color='RED'> s </FONT>"
  10. elif data[0][2] == "g":
  11.       print "<font color='GREEN'> s </FONT>"
  12.  
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
Expand|Select|Wrap|Line Numbers
  1. for line in open("input_test_comma.txt"):
  2.      cue,response,color = line.strip().split(", ")
  3.      if color == "r":
  4.                 ....
  5.      elif color == "g":
  6.                 .....
  7.  
  8.  
Mar 31 '07 #2
dshimer
136 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. >>> datalist=[]
  2. >>> for line in open("/tmp/test.txt"):
  3. ...     datalist.append(line.strip().split(', '))
  4. ... 
  5.  
Then to walk through the values
Expand|Select|Wrap|Line Numbers
  1. >>> for data in datalist:
  2. ...     print data
  3. ... 
  4. ['CAT', 'MEOW', 'r']
  5. ['TABLE', 'CHAIR', 'g']
  6. ['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.
Mar 31 '07 #3
ghostdog74
511 Expert 256MB
....
. 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...
Mar 31 '07 #4
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
Mar 31 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
5
by: Ben | last post by:
I'm currently trying to develop a demonstrator in python for an ontology of a football team. At present all the fit players are exported to a text document. The program reads the document in and...
1
by: Patrícia Martins | last post by:
Hi, I would like to know if it's possible to validate attributes in a situation like this: <TUPLE> <LIST type="type1">Bla</LIST> <LIST type="type2">Ble</LIST> <ATOM type="type3">Bli</ATOM>...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
4
by: Mr.Rech | last post by:
Hi all, I was writing a simple class when I get a strange error message that I can't understand. Hopefully someone could help me here. My class's init method takes a list of lists as input...
6
by: Dag | last post by:
I have an application which works with lists of tuples of the form (id_nr,'text','more text',1 or 0). I'll have maybe 20-50 or so of these lists containing anywhere from 3 to over 30000 tuples. ...
5
by: Nathan Harmston | last post by:
Hi, Sorry if the subject line of post is wrong, but I think that is what this is called. I want to create objects with class Coconuts(object): def __init__(self, a, b, *args, **kwargs):...
5
by: TheSeeker | last post by:
Hi, I have run into something I would like to do, but am not sure how to code it up. I would like to perform 'set-like' operations (union, intersection, etc) on a set of objects, but have the...
4
by: Casey | last post by:
Hi, I have some classes that print variable outputs depending on their internal state, like so: def __str__(self): out = if self.opt1: out += if self.opt2: out += ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.