I have a tree with the following structure:
my_hash_pop = {
"Europe" : {
"France" : {
"Paris" : 2220445,
"Lille" : 225789,
"Lyon" : 506615 },
"Germany" : {
"Berlin" : 3520031,
"Munchen" : 1544041,
"Dresden" : 540000 },
},
"South America" : {
"Brasil" : {
"Sao Paulo" : 11895893,
"Rio de Janeiro" : 6093472 },
"Argentina" : {
"Salta" : 535303,
"Buenos Aires" : 3090900 },
},
}
I would like to convert this structure to CSV using python:
Europe;Germany;Berlin;3520031
Europe;Germany;Munchen;1544041
Europe;Germany;Dresden;540000
Europe;France;Paris;2220445
Europe;France;Lyon;506615
Europe;France;Lille;225789
South America;Argentina;Buenos Aires;3090900
South America;Argentina;Salta;3090900
South America;Brasil;Sao Paulo;11895893
South America;Brasil;Rio de Janeiro;6093472
Since my tree contains a large number of leaves in real life (not in this example, obviously), the script conversion that I use takes a lot of time. I am trying to find a more effective way to do the conversion. Here is what I tried:
First method: concatenate a line on each sheet:
start_1 = time.time()
data_to_write = ""
for region in my_hash_pop:
for country in my_hash_pop[region]:
for city in my_hash_pop[region][country]:
data_to_write += region+";"+country+";"+city+";"+str(my_hash_pop[region][country][city])+"\n"
filename = "my_test_1.csv"
with open("my_test_1.csv", 'w+') as outfile:
outfile.write(data_to_write)
outfile.close()
end_1 = time.time()
print("---> METHOD 1 : Write all took " + str(end_1 - start_1) + "s")
Second method: combine string with "breakpoints"
start_2 = time.time()
data_to_write = ""
for region in my_hash_pop:
region_to_write = ""
for country in my_hash_pop[region]:
country_to_write = ""
for city in my_hash_pop[region][country]:
city_to_write = region+";"+country+";"+city+";"+str(my_hash_pop[region][country][city])+"\n"
country_to_write += city_to_write
region_to_write += country_to_write
data_to_write += region_to_write
filename = "my_test_2.csv"
with open("my_test_2.csv", 'w+') as outfile:
outfile.write(data_to_write)
outfile.close()
end_2 = time.time()
print("---> METHOD 2 : Write all took " + str(end_2 - start_2) + "s")
Third method: with a Writer object
import csv
start_3 = time.time()
with open("my_test_3.csv", 'w+') as outfile:
del_char = b";"
w = csv.writer(outfile, delimiter=del_char)
for region in my_hash_pop:
for country in my_hash_pop[region]:
for city in my_hash_pop[region][country]:
w.writerow([region, country, city, str(my_hash_pop[region][country][city])])
end_3 = time.time()
print("---> METHOD 3 : Write all took " + str(end_3 - start_3) + "s")
, , , 1 . 2 3, , ( 3 )
:
:
!