| | |
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: 150
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 approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






