function - Python homework help: issues with finding least common string in csv file -
function - Python homework help: issues with finding least common string in csv file -
i issues homework create programme reads csv files reply question, unfortunately gasp of isn’t strong other. have written bulk of code , thought clear clear syntax incorrect. according assignment have reply question:
count number of (t's) in field [correct] how many times to the lowest degree mutual string appear in field [gas] find sum of values in field [quant] less (408) how many values in 'code' field not match format 9999(x9+)9? what average value of numbers in field [age] in range (30) , (107) inclusive find sum of numbers in field [length] between (2.482) , (6.428) inclusive count lines gas's have value (nitrogen) or quant less 318so wrote code best of ability. code is
stage 2 function
import string def getfile(): filename = input('filename: ') #the file name should .csv file = open(filename, 'r') firstline = true line = file.readline() if line == none or line == '': homecoming none if firstline: # not want read field names line = file.readline() # there more read firstline = false # skip them. code assuems homecoming file #count number of (t's) in field [correct] def calct(correct): global tcount found = false ch in correct:#look @ each character in turn if ch in 'tt': found = true if found: tcount +=1 #how many times to the lowest degree mutual string appear in field [gas] def least_string(gas): smallest = 100000 if gas in gas_dict: gas_dict[gas] += 1 else: gas_dict[gas] = 1 key in gas_dict: if gas_dict[key] < smallest: smallest = gas_dict[key] answers.append(key) #find sum of values in field [quant] less (408) def sum_quant(quant): global qsum if quant < 408: qsum += quant #how many values in 'code' field not match format 9999(x9+)9? def checkstring(astring): if len(astring) != 10: homecoming false if not astring[0] in string.digits: homecoming false if not astring[1] in string.digits: homecoming false if not astring[2] in string.digits: homecoming false if not astring[3] in string.digits: homecoming false if not astring[4]=='(': homecoming false if not astring[5] in string.ascii_lowercase: homecoming false if not astring[6] in string.digits: homecoming false if not astring[7]=='+': homecoming false if not astring[8]==')': homecoming false if not astring[9] in string.digits: homecoming false homecoming true #what average value of numbers in field [age] in range (30) , (107) inclusive def average_age(age): global tage, agecount if age >= 30 , age <=107: tage += age agecount += 1 #find sum of numbers in field [length] between (2.482) , (6.428) inclusive def sum_length(leng): global lensum if leng >= 2.482 , leng <= 6.428: lensum += leng #count lines gas's have value (nitrogen) *or* quant less 318 def calcgas(gas, quant): global clines if gas == 'nitrogen' or quant < 318: clines += 1 def processline(line): line = line.strip() fields = line.split(',') right = fields[0] gas = fields[1] quant = int(fields[2]) code = fields[3] if checkstring(code): global ccount ccount += 1 age = int(fields[4]) leng = float(fields[5]) calct(correct) sum_length(leng) calcgas(gas, quant) average_age(age) sum_quant(quant) least_string(gas) def processfile(data): line in data: processline(line) data.close() def displayresults(): print(''' stage 1 function number of (t) in field [correct]:%d %s to the lowest degree mutual string appear in field [gas]:%s sum of values in field [quant] less (408): %d values in code field not match format 9999(x9+)9: %d average value of numbers in field[age] in range(30)and(107):%0.2f sum of numbers in field [length] between (2.482) , (6.428):%0.3f lines gas have value (nitrogen) *or* quant less 318: %d '''%(tcount,gas_dict,answers,qsum,ccount,((tage/agecount)),lensum,clines)) tcount = 0 qsum = 0 gas_dict = {} answers =[] ccount = 0 agecount = 0 tage = 0 lensum = 0 clines = 0 myfile = getfile() processfile(myfile) displayresults()
so when run code, come in in filename (3215402a.csv). works out fine. returns whole list of gas
to the lowest degree mutual string appear in field [gas]:['helium', ' chlorine', 'nitrogen', 'nitrogen', 'oxygen', 'oxygen', 'nitrogen', 'oxygen', 'nitrogen', 'oxygen', 'argon', 'oxygen', 'hydrogen', 'oxygen', 'hydrogen', 'oxygen', 'hydrogen', 'oxygen', 'hydrogen', 'xenon', 'oxygen', 'hydrogen', 'xenon', 'oxygen', 'hydrogen', 'xenon', 'carbondioxide', 'carbondioxide', 'carbondioxide', 'carbondioxide', 'carbondioxide']
the code should print out
the to the lowest degree mutual string appear in field [gas]: ['carbondioxide', 'argon', 'xenon']
there 1 point in programming process worked
{'methane': 3, 'hydrogen': 2, 'argon': 1, 'carbondioxide': 1, 'nitrogen': 3, ' chlorine': 3, 'oxygen': 3, 'xenon': 1, 'helium': 2}
what in world doing wrong? have looked @ plenty of topics on stack overflow , used different method, either error or values won’t returned. can @ can prepare code?
thank much , appreciate help!
def processfile(data): global least_gas line in data: processline(line) minkey = min(gas_dict, key=gas_dict.get) least_gas = gas_dict[minkey] data.close()
python function file csv python-3.3
Comments
Post a Comment