| | |
Color Dictionary
Thread Solved
![]() |
I have a color data file that gives the color name and its RGB values, for instance like this:
I like to create a color dictionary from this file that looks like this:
How can I best do that?
Python Syntax (Toggle Plain Text)
red RGB(255,0,0) green RGB(0,128,0) blue RGB(0,0,255) brown RGB(165,42,42) gold RGB(255,215,0) maroon RGB(128,0,0)
Python Syntax (Toggle Plain Text)
{'red': (255,0,0), 'green': (0,128,0), 'blue': (0,0,255), ...}
No one died when Clinton lied.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 149
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
[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
![]() |
Similar Threads
- HELP!! Monitor is showing RED color in place of WHITE (Monitors, Displays and Video Cards)
- Microsoft Word Dictionary Problem (Windows Software)
- obnoxious color codes (DaniWeb Community Feedback)
- Dictionary Sorting? (Python)
- HELP!! Monitor is showing RED color in place of WHITE (Monitors, Displays and Video Cards)
Other Threads in the Python Forum
- Previous Thread: Prevent Code Errors?
- Next Thread: Graphics User Interface for Beginners
| Thread Tools | Search this Thread |
abrupt alarm ansi anti apache approximation array assignment backend beginner binary bluetooth builtin calculator character cmd converter countpasswordentry curved customdialog cx-freeze dan08 data decimals dictionary directory exe file float format function gnu halp heads homework http ideas inches input itunes java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook pointer prime programming push py2exe pygame python random recursion redirect schedule screensaverloopinactive script scrolledtext software sqlite ssh statictext statistics string strings sudokusolver terminal text thread time tlapse tuple twoup ubuntu unicode urllib urllib2 variable ventrilo webservice wikipedia wordgame write wxpython xlib






