Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- import os.path
- fileName=None
- if __name__=="__main__":
- try:
- fileName=argv[1]
- isFile(fileName)
- pass
- except Exception as e:
- print("You must provide a valid filename as parameter")
- raise
- def isFile(fileName):
- if(not os.path.isfile(fileName)):
- raise ValueError("You must provide a valid filename as parameter")
- print fileName
- def readCsvAndCountPercentPerFormItemFromGoogleForms(fileName):
- times={}
- totalRows=0
- with open(fileName,'r') as csvfile:
- csvReader=csv.reader(csvfile);
- for row in csvreader:
- value=row[1]
- if(value in times.values()):
- times[value]+=1
- else:
- times[value]=1
- totalRows+=1
- return calculateDictionaryAsPercent(times,totalRows)
- def calculateDictionaryAsPercent(times,totalRows):
- if(totalRows==0):
- raise ValueError("The file does not contain any rows")
- for key,val in times.items():
- times[key]=(val/totalRows)*100
- return times
- finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
- print finalTimes
- Traceback (most recent call last):
- File "csv.py", line 1, in <module>
- import csv
- File "/home/pcmagas/Kwdikas/python/csv.py", line 54, in <module>
- finalTimes=readCsvAndCountPercentPerFormItemFromGoogleForms(fileName)
- File "/home/pcmagas/Kwdikas/python/csv.py", line 26, in readCsvAndCountPercentPerFormItemFromGoogleForms
- with open(fileName,'r') as csvfile:
- TypeError: coercing to Unicode: need string or buffer, NoneType found
- if __name__=="__main__":
- try:
- fileName=argv[1]
- isFile(fileName)
- pass
- except Exception as e:
- print("You must provide a valid filename as parameter")
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement