Explain what the following Python program is used for and how it works? filename
ID: 3585772 • Letter: E
Question
Explain what the following Python program is used for and how it works?
filename = raw_input("Please enter your input filename: ")
f = open(filename)
csv_f = csv.reader(f)
link_fre = dict()
nameList = row[2].split(', ')
nameList.sort()
combination_names = itertools.combinations(nameList, 2)
for combination in combination_names:
if combination in link_fre:
link_fre[combination] += 1
else:
link_fre[combination] = 1
new_f = open('network.csv', 'wt')
try:
writer = csv.writer(new_f)
writer.writerow( ('source', 'target', 'weight') )
for key in link_fre:
writer.writerow( (key[0], key[1], link_fre[key]) )
finally:
new_f.close()
Explanation / Answer
This program is basically reading content from one csv file and calculating frequency of source and wirting the same thing in network.csv output file. Below is the explanation of each line of this program:
#Asking user to enter the file name of csv file to read the content
filename = raw_input("Please enter your input filename: ")
#Here opening the file using open function and passing the user given file name
f = open(filename)
#Here using csv library to read csv file so that content will be easy to read in comma(,) separated values
csv_f = csv.reader(f)
#creating a new instance of dict. dict is key/value hash table structure in python
#contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }
link_fre = dict()
#here getting second row of csv file and splitting the content based on comma(,) and storing the splitted content in an array
nameList = row[2].split(', ')
#Sorting the array using the sort API of python
nameList.sort()
#combinations allows to get the all combinations of the contents of the list or array in python
#Here we are getting all the combination of the sorted nameList
combination_names = itertools.combinations(nameList, 2)
#iterating over all the combinations created above
for combination in combination_names:
#checking if combination is present in the dict named as link_fre which we created above
#if it is there then we are increasing the count by 1
if combination in link_fre:
link_fre[combination] += 1
#first time when link_free dict is empty then it will go in else condition and then we will add first entry of combination
else:
link_fre[combination] = 1
#opening the network.csv in "wt" means write mode so that we can write something in this file
new_f = open('network.csv', 'wt')
try:
#getting writer instance of csv file
writer = csv.writer(new_f)
#writing the row as "souce","target","weight"
writer.writerow( ('source', 'target', 'weight') )
#iterating over all the keys in link_fre dict
for key in link_fre:
#writing in the network.csv wile for each link_fre key and value
writer.writerow( (key[0], key[1], link_fre[key]) )
finally:
#In the end we should close the file so that it can be used by other resources
new_f.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.