contents = "skill,,,,,,,,\n,,,,,,,,\nIP address: 98.16.5.37,,,,,,,,\nAndrew,31,male,,,,,,\n,Ships,Aimed For,Landed At,Result,Latency,Moves,Happinness,WantsMore\ntrial,4:5|4:6,02:01,00:00,fullMiss,11.977,1,35,24\ntrial,1:6|2:6,02:02,02:02,hit,12.113,1,99,28" stringOfResults = contents.strip() #This is removing any leading or lagging white space stringToList = stringOfResults.split('\n') #This is creating a long uni dimentional list with each item a row from the CSV #print(stringToList) #print(stringToList[0]) #This is printing the condition - why I don't know - a test? condition = stringToList[0] #Note the assumption that 'cpndition will always be in this location. unformattedIP = stringToList[2] #Note the assumption that IP will always be here ipAddresses = [] IP = "" for character in unformattedIP: #So this bit of code doesn't work - it doesn't return an error, just nothing happens. if isinstance(character, int): #It recognises each character? IP = IP + character if IP not in ipAddresses: ipAddresses.append(IP) position = 0 #Random position counter for paranoia name = None age = 0 gender = 0 trial = 0 fullMiss = 0 nearMiss = 0 hit = 0 fullMissHappiness = 0 fullMissWantsMore = 0 nearMissHappiness = 0 nearMissWantsMore = 0 hitHappiness = 0 hitWantsMore = 0 maxHappiness = -1 maxHappinessTrial = 0 minHappiness = 10000 minHappinessTrial = 0 for i in range(len(stringToList)): line = stringToList[i].split(',') #Now each line is a list if 'male' in line or 'Male' in line: #Assigning gender 1 for male and 2 for female, using gender as the unique identifier for this line to extract name and age. gender = 1 name = line[0] age = line[1] if 'female' in line or 'Female' in line: gender = 2 name = line[0] age = line[1] if 'trial' in line: trial += 1 #Trying to count the trials if 'hit' in line: hit += 1 #Counting hits and their associated happiness and desire for a future move hitHappiness += int(line[7]) hitWantsMore += int(line[8]) if 'nearMiss' in line: nearMiss += 1 #Counting near misses and their acosiated happiness and desire for a future move nearMissHappiness += int(line[7]) nearMissWantsMore += int(line[8]) if 'fullMiss' in line: fullMiss += 1 #Counting full misses and their ascoiated happiness and desire for a future move fullMissHappiness += int(line[7]) fullMissWantsMore += int(line[8]) if 'trial' in line and int(line[7]) < minHappiness: #Not sure if this counts as hard coding as am assuming the structure won't change minHappiness = int(line[7]) #Finding a Boolean whereby the trial number is set by a change in the value for minimum happiness minHappinessTrial = trial if 'trial' in line and int(line[7]) > maxHappiness: #Not sure if this is hard coding, am assuming that happiness will always be in the place it is in currently maxHappiness = int(line[7]) #Finding a Boolean whereby the trial number is set by a change in the value for maximum happiness maxHappinessTrial = trial #If you cannot get the above to work you will have to create an empty list prior to the if functions, append the list with line[7]. #After the completion of the loop you will have to 'find index of max value' (assign to variable, will be trial number), then assign the value at that index to the variable max happy. #repeat for minimum #print(line) position += 1 # James' printing print(name) print(age) print(gender) print(hit) print(nearMiss) print(minHappiness) print(maxHappiness)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question