This script gets data from a csv file that contains colors data in the form:
air_force_blue_raf,"Air Force Blue (Raf)",#5d8aa8,93,138,168 air_force_blue_usaf,"Air Force Blue (Usaf)",#00308f,0,48,143 air_superiority_blue,"Air Superiority Blue",#72a0c1,114,160,193 alabama_crimson,"Alabama Crimson",#a32638,163,38,56 alice_blue,"Alice Blue",#f0f8ff,240,248,255 alizarin_crimson,"Alizarin Crimson",#e32636,227,38,54
Now we will get the data opening the file and putting it into a variable called content:
Get the content of the csv file
def get_csv(): with open("colors.csv") as file: content = file.readlines() return content
Using get_csv se will get the data into colors.csv. We can make it more ‘abstract’ changing it into this, so that is usable for any csv file, instead of the specific one named colors.csv
def get_csv(file): with open(file) as file: content = file.readlines() return content
In this case you got to specify the name of the file when you call the function get_csv with:
content = get_csv("colors.csv")
If we want to write more readable code we can also enhance the script with some hints:
def get_csv(file: str) -> list: with open(file) as file: content = file.readlines() return content
So now we know that we must pass a parameter that is a string and that the function will return a list out of the file csv. If you wanna be more explicit you can add this:
def get_csv(csv_filename: str) -> list: ''' return a list with an item for each line of the file ''' with open(csv_filename) as file: itemslist = file.readlines() return itemslist
Ok, now I thinks is really readable with all the names that have sense and give the idea of what they are made for.
Creating the dictionary with data we need
Now we want to put this data into a dictionary, so that it is easy to use them for our apps (we will use it for a colorpicker app the tutorial of which will be posted in a couple of days or even less).
def create_dic(content): coldic = {} for k in content[:5]: id,name,hx,r,g,b = k.split(",") name = name.replace("\"","") coldic[name] = hx return coldic
Use the functions
Now we get the data we need and display them to see what we get.
content = get_csv("colors.csv") dic = create_dic(content) print(*dic.items(), sep="\n")
The result will be this:
On the right you see the result. We got a dictionary (displayed as tuples as we used print(*…)).
Ok, it’s all fine. As I said, we will use these 2 functions in an app to pick colors with tkinter in the next post.
See ya you all.
The Whole code
# from csv to dictionary def get_csv(csv_filename: str) -> list: ''' return a list with an item for each line of the file ''' with open(csv_filename) as file: itemslist = file.readlines() return itemslist def create_dic(content): coldic = {} for k in content[:5]: id,name,hx,r,g,b = k.split(",") name = name.replace("\"","") coldic[name] = hx return coldic content = get_csv("colors.csv") dic = create_dic(content) print(*dic.items(), sep="\n")
The repository with the code and the csv file
https://github.com/formazione/tkinter_tutorial.git
The file of this post is called color_names.py
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame