# define the name of the js file
new_file = "data2014.js"
# open and start to write data to the js file
new_dt = open(new_file, "w+")
filename = "yob2014.txt" # the txt file name
print 'Start to build the data file....'
with open(filename) as f:
new_dt.write("var data_rows = [['Names', null, 0],\n") # add the root node
new_dt.write("['Female', 'Names', null],\n") # add the parent node for female names
new_dt.write("['Male', 'Names', null],\n") # add the parent node for male names
male_count = 0
female_count = 0
for line in f:
line = line.replace("\n", "") # delete the line breaks
a = line.split(",") # split the line by comma(,)
# reconstruct the txt line into treemap required format
if a[1] == 'F': # if this is a female name
female_count = female_count + 1
if female_count <= 50: # if total number of female names are not over 50
new_dt.write("['" + a[0] + "', 'Female', " + a[2] + "],\n") # add the name to the datatable
else: # if this is a male name
male_count = male_count + 1
if male_count <= 50:
new_dt.write("['" + a[0] + "', 'Male', " + a[2] + "],\n")
new_dt.write("];") # end of the js array
new_dt.close() # when writing is done, close the js file
print 'Data file is completed!'
If you get an error like No such file or directory: ‘yob2014.txt’, check that the file is actually in the folder. Also, check if the working directory is the folder containing the file. If not, please read this post to learn how to change the working directory of Canopy.
External Resources about Python Files I/O
