Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import csv
  2. import os.path
  3.  
  4. fileName=None
  5.  
  6. if __name__=="__main__":
  7. try:
  8. fileName=argv[1]
  9. isFile(fileName)
  10. pass
  11. except Exception as e:
  12. print("You must provide a valid filename as parameter")
  13. raise
  14.  
  15. def isFile(fileName):
  16. if(not os.path.isfile(fileName)):
  17. raise ValueError("You must provide a valid filename as parameter")
  18.  
  19. print fileName
  20.  
  21.  
  22. def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName):
  23. times={}
  24. totalRows=0
  25. with open(fileName,'r') as csvfile:
  26.  
  27. csvReader=csv.reader(csvfile);
  28.  
  29. for row in csvreader:
  30.  
  31. value=row[1]
  32.  
  33. if(value in times.values()):
  34. times[value]+=1
  35. else:
  36. times[value]=1
  37.  
  38. totalRows+=1
  39.  
  40. return calculateDictionaryAsPercent(times,totalRows)
  41.  
  42. def calculateDictionaryAsPercent(times,totalRows):
  43.  
  44. if(totalRows==0):
  45. raise ValueError("The file does not contain any rows")
  46.  
  47. for key,val in times.items():
  48. times[key]=(val/totalRows)*100
  49.  
  50. return times
  51.  
  52.  
  53. finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
  54.  
  55. print finalTimes
  56.  
  57. Traceback (most recent call last):
  58. File "csv.py", line 1, in <module>
  59. import csv
  60. File "/home/pcmagas/Kwdikas/python/csv.py", line 54, in <module>
  61. finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
  62. File "/home/pcmagas/Kwdikas/python/csv.py", line 26, in readCsvAndCountPercentPerFormItemFromGoogleForms
  63. with open(fileName,'r') as csvfile:
  64. TypeError: coercing to Unicode: need string or buffer, NoneType found
  65.  
  66. if __name__=="__main__":
  67. try:
  68. fileName=argv[1]
  69. isFile(fileName)
  70. pass
  71. except Exception as e:
  72. print("You must provide a valid filename as parameter")
  73. raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement