I have a .txt file that has the following information in it that displays a users name and then 3 scores that they have scored in a quiz:
callum,10,7,9
carl,10,10,10
hollie,1,4,7
brad,4,8,3
kyle,7,2,0
I would like to sort it into alphabetical order displaying the users highest score after their name.
Read file content.
Use readlines() method to read lines from file.
Use split() to get Name and score.
Add in dictionary: Name is Key and Value is total score.
Get all keys from the result dictionary.
User sort() method to sort list by alphabets.
Print result by alphabets order.
Code
p = "/home/vivek/Desktop/test_input.txt"
result = {}
with open(p, "rb") as fp:
for i in fp.readlines():
tmp = i.split(",")
try:
result[(tmp[0])] = eval(tmp[1]) + eval(tmp[2]) + eval(tmp[3])
except:
pass
alphabetical_name = result.keys()
alphabetical_name.sort()
for i in alphabetical_name:
print "Name:%s, Highest score: %d"%(i, result[i])
Output:
$ python test.py
Name:brad, Highest score: 15
Name:callum, Highest score: 26
Name:carl, Highest score: 30
Name:hollie, Highest score: 12
Name:kyle, Highest score: 9
So i would first of all isolate all the lines:
with open('filename') as f:
lines = f.readlines()
So i will continue assuming I have a list called lines with the following content:
lines = ["callum,10,7,9","carl,10,10,10","hollie,1,4,7","brad,4,8,3","kyle,7,2,0"]
Then I will firstly sort the line by name
lines = sorted(lines)
Then for each line you want to isolate the marks, sort them and print them back:
for line in lines:
#name is what there is before the first comma
name = line[:line.find(",")]
#marks are what there is after the second comma and are comma separated
marks = line[line.find(",")+1:].split(",")
#sort the marks
marks = sorted(marks,key=int)
#if you want to print only the highest
print "%s,%s"%(name,marks[-1])
Related
I am not sure how to get my file to sort and display the top 5 scores from my text file.
Text file below :
24fred
23alan
24bert
28dan
11orange
17purple
16dave
22andy
The code which I am using to write to the file.
Tried using sort but can't get it to display only the top 5 scores.
file = open("Score.txt", "a")
file.write(str(name))
file.write(str(Score))
file.write("\n")
file.close
the file will print out sorted and only showing the top 5
You can use the following sample:
import re
pat = re.compile(r"^(\d+)(\D+)$")
def sort_crit(i):
m = pat.match(i)
return - int(m.group(1)), m.group(2)
with open("Score.txt",'r') as f:
lines = [line.rstrip() for line in f]
lines.sort(key = sort_crit)
with open('Sorted_score.txt', 'w') as f:
for item in lines:
f.write("%s\n" % item)
input:
$ more Score.txt
24fred
23alan
24bert
28dan
28abc
11orange
17purple
16dave
22andy
output:
$ more Sorted_score.txt
28abc
28dan
24bert
24fred
23alan
22andy
17purple
16dave
11orange
Explanations:
re.compile(r"^(\d+)(\D+)$") will be used to extract individually the score and the name
sort_crit(i) will return the double sorting criteria based first on the score in reverse order (note the -), followed by the name in alphabetic order
You open the input file and store all the lines in an array
You sort the lines using the sorting function you have defined
you output them in a new file
I have to read a list of 12 grades from a text file. Each grade is on a new line in the text file. I have to find the lowest grade and drop it(in the text file as well), then find the average score of the grades. I am having trouble making the text values into integer values so I can find the average. I also can't figure out how to find the lowest grade and drop it.
Here is my code:
try:
homeworkFile = open('homework.txt', 'r')
except:
print("Error: invalid file")
else:
lines = homeworkFile.readlines()
homeworkFile.close()
homeworkFile = open('homework.txt', 'w')
for line in lines:
Thanks for any help you can give!
So this is just one way to take all of your values and calculate the average.
input_file = input("enter file name: ")
open_file = open(input_file, 'r')
Here I just put all of your values into notepad and read through it
grades_list = []
for grade in open_file:
grade_format = grade.strip() #how to remove extra blank space
grades_list.append(grade_format)
Then I used a for-loop to go through each line and put the grades into a list
grade_convert = [] #new list for all of the converted values
for grade in grades_list:
convert = float(grade) #convert each value in the list into a float
grade_convert.append(convert)
I used another list for converted each value
grade_convert = sorted(grade_convert) #very first element will be the lowest
grade_convert.pop(0) #permanently removes the very first item
grade_total = 0 #set a counter
for grade in grade_convert:
grade_total += grade #add all of the values to the counter
grade_average = grade_total / len(grade_convert) #len is number of items
print(grade_average)
grades_list = []
for line in lines :`
grade = float(line)
grades_list.append(grade)
grades_list.sort()
lowest_grade = grades_list[0] #There is the lowest grade
This is one way you can structure your logic. You need to convert values to float pre-processing. Using with is recommended instead of using open and close explicitly.
import os
file = 'homework.txt'
# check if file exists
assert os.path.exists(file), "File does not exist: {0}".format(file)
# use with to open/close file implicitly
with open(mystr, 'w') as file_in:
lines = homeworkFile.readlines()
grades = sorted(float(line) for line in lines)
# drop lowest trade
del grades[0]
I have a file in the below format
.aaa b/b
.ddd e/e
.fff h/h
.lop m/n
I'm trying to read this file. My desired output is if I find ".aaa" I should get b/b, if I find ".ddd" I should get e/e and so on.
I know how to fetch 1st column and 2nd column but I don't know how to compare them and fetch the value. This is what I've written.
file = open('some_file.txt')
for line in file:
fields = line.strip().split()
print (fields[0]) #This will give 1st column
print (fields[1]) # This will give 2nd column
This is not the right way of doing things. What approach follow?
Any time you want to do lookups, a dictionary is going to be your friend.
You could write a function to load the data into a dictionary:
def load_data(filename):
result = dict()
with open(filename, 'r') as f:
for line in f:
k,v = line.strip().split() # will fail if not exactly 2 fields
result[k] = v
return result
And then use it to perform your lookups like this:
data = load_data('foo.txt')
print data['.aaa']
It sounds like what you may want is to build a dictionary mapping column 1 to column 2. You could try:
file = open('some_file.txt')
field_dict = {}
for line in file:
fields = line.strip().split()
field_dict[fields[0]] = fields[1]
Then in your other code, when you see '.ddd' you can simply get the reference from the dictionary (e.g. field_dict['.ddd'] should return 'e/e')
Just do splitting on each line according to the spaces and check whether the first item matches the word you gave. If so then do printing the second item from the list.
word = input("Enter the word to search : ")
with open(file) as f:
for line in f:
m = line.strip().split()
if m[0] == word:
print m[1]
file=sorted(file)
the dictionary data(name, score avg) is collected earlier and stored in the file. this code then re opens the file and stores the data from it in a variable(file). I need this data from the file to be sorted by value so if the data in the file is:
james: 5
tim: 8
it will sort so that tim goes at the top as he has a higher scoreavg. How would i go about doing this?
also the dictionary is there so that name and score stay together when sorted
import collections
dict = {}
# Assume file format is "name:score" for each line.
with open("classa2.txt", "r") as f:
for line in f:
line = line.split(':')
dict[line[0]] = line[1]
# Sort by value (score)
od = collections.OrderedDict(sorted(dict.items(), key=lambda t: t[1]))
# Write new results
with open("classa2.txt", "w") as f:
for key in od:
f.write(key + ':' + str(od[key]) + "\n")
print key + ":" + str(od[key])
Apparently the data file looks like
tim: ["4.0", "2.9", "3.4"]
james: ["3.8", "3.8"]
clara: ["2.6", "3.5", "3.2"]
in which case you need something like
from ast import literal_eval
def sort_key(line):
# split "name: scores" into ["name", " scores"]
name, score_str = line.split(":")
# convert " [\"1.9\", \"3.8\"]" into ["1.9", "3.8"]
scores = literal_eval(score_str.strip())
# convert ["1.9", "3.8"] into [1.9, 3.8]
scores = [float(f) for f in scores]
# calculate average score
avg = sum(scores) / len(scores)
return avg
then you can
# read data from file
with open("classa2.txt") as inf:
lines = [line.rstrip() for line in inf]
# sort highest scores first
lines.sort(key=sort_key, reverse=True)
print("\n".join(lines))
which results in
james: ["3.8", "3.8"]
tim: ["4.0", "2.9", "3.4"]
clara: ["2.6", "3.5", "3.2"]
I am attempting to run a python program that can run a dictionary from a file with a list of words with each word given a score and standard deviation. My program looks like this:
theFile = open('word-happiness.csv' , 'r')
theFile.close()
def make_happiness_table(filename):
'''make_happiness_table: string -> dict
creates a dictionary of happiness scores from the given file'''
with open(filename) as f:
d = dict( line.split(' ') for line in f)
return d
make_happiness_table("word-happiness.csv")
table = make_happiness_table("word-happiness.csv")
(score, stddev) = table['hunger']
print("the score for 'hunger' is %f" % score)
My .csv file is in the form
word{TAB}score{TAB}standard_deviation
and I am trying to create the dictionary in that way. How can I create such a dictionary so that I can print a word such as 'hunger' from the function and get its score and std deviation?
def make_happiness_table(filename):
with open(filename) as f:
d = dict()
for line in f:
word,score,std = line.split() #splits on any consecutive runs of whitspace
d[word]=score,std # May want to make floats: `d[word] = float(score),float(std)`
return d
Note that if your word can have a tab character in it, but you're guaranteed that you only have 3 fields (word, score, std), you can split the string from the right (str.rsplit), only splitting twice (resulting in 3 fields at the end). e.g. word,score,std = line.rsplit(None,2).
As mentioned in the comments above, you can also use the csv module to read these sorts of files -- csv really shines if your fields can be "quoted". e.g.:
"this is field 0" "this is field 1" "this is field 2"
If you don't have that scenario, then I find that str.split works just fine.
Also, unrelated, but your code calls make_happiness_table twice (the first time you don't assign the return value to anything). The first call is useless (all it does is read the file and build a dictionary which you can never use). Finally, opening and closeing theFile at the beginning of your script is also just a waste since you don't do anything with the file there.
If you are sure your word will not have space, you can just split the line e.g.
word, score, stddev = line.split()
But if word can have space use tab char \t to split e.g.
word, score, stddev = line.split('\t')
But for a very generic case when word may have tab itself use the csv module
reader = csv.reader(filename, dialect='excel-tab')
for word, score, stddev in reader:
...
and then you can create dict of word and score, stddev e.g.
word_dict[word] = (score, stddev)