For some months I've talked about creating a database with Python Dictionary and some TXT, and you can really! It's very interesting to be able to use the Power of the dictionary as a database.
Dictionaries have the feature that they are stored directly in RAM which makes any call with their methods almost instantaneous.
The structure of the Python variables is interesting, for example we have:
Chains, Lists and Tuples, share many common methods, but they have their differences, for example:
The dictionaries for me are another level in programming with Python, it's like doing magic, creating dictionaries that really work as we want.
Today we are going to see some examples of code that I have used in my programs, I have always used a simple code to add data as if it were a Database with my Dictionary in a TXT, I charge it and it works very fast and it is re-usable.
I'm talking about my letter counter, I remember when I had to do it in Coursera in the specialization and now I see how much it has helped me until today.
This time we are going to add a file that stores all the entries that we pass and keeps the repetition of the letters, so we can add and add data and function as a Pythonica database, here the code:
file = open("BD_count_letter.txt")
data = file.readline()
dict_charts = data.strip()
dict_charts = eval(dict_charts)
if dict_charts != "":
dict_charts = eval(dict_charts)
else:
dict_charts = {}
string = raw_input("WRITE A CHARACTER CHAIN:")
for character in string:
if character not in dict_charts:
dict_charts[character] = 1
else:
dict_charts[character] = dict_charts[character] +1
print dict_charts
file = open("BD_count_letter.txt", "w")
file.write(str(dict_charts))
file.close ()
file = open("BD_count_letter.txt")
data = file.readline()
file = open("BD_count_letter.txt") can have any name, but I put that, must be created eye.dict_charts = data.strip()
dict_charts = eval(dict_charts)
if dict_charts != "":
dict_charts = eval(dict_charts)
else:
dict_charts = {}
string = raw_input("WRITE A CHARACTER CHAIN:")
for character in string:
if character not in dict_charts:
dict_charts[character] = 1
else:
dict_charts[character] = dict_charts[character] +1
print dict_charts
file = open("BD_count_letter.txt", "w")
file.write(str(dict_charts))
file.close ()