Color Dictionary

Thread Solved

Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Color Dictionary

 
0
  #1
Nov 10th, 2006
I have a color data file that gives the color name and its RGB values, for instance like this:
  1.  
  2. red RGB(255,0,0)
  3. green RGB(0,128,0)
  4. blue RGB(0,0,255)
  5. brown RGB(165,42,42)
  6. gold RGB(255,215,0)
  7. maroon RGB(128,0,0)
I like to create a color dictionary from this file that looks like this:
  1.  
  2. {'red': (255,0,0), 'green': (0,128,0), 'blue': (0,0,255), ...}
How can I best do that?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Color Dictionary

 
0
  #2
Nov 11th, 2006
If the data are packaged as neatly as your file shows, then I would recommend using .split() and then eval() to convert the parenthesized string to a tuple:

[php]
f = open("my_data")
colors = {}
for line in f:
line = line.strip('\n')
name, value = line.split(' RGB ')
colors[name] = eval(value) # converts string to tuple
[/php]

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Color Dictionary

 
0
  #3
Nov 12th, 2006
Jeff, thanks. I had to change the split separator from ' RGB ' to ' RGB' to make it work.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC