Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to import grades into a text file in python

students = list() # initialize an accumulator list

with open("stuff2.txt") as infile:
    for line in infile:
        data = line.strip().split(" ")
        # strip removes ending and beginning whitespace e.g. the ending 
 and etc
        datadict = {}
        datadict['first'] = data[0]
        datadict['last'] = data[1]
        datadict['grades'] = data[2:]
        students.append(datadict)
        # this can all be done in one line, but it's much clearer this way
# after this, all your students are in `students`, each entry in `students` is a
# dictionary with keys `first`, `last`, and `grades`.

# OUTPUT
with open("newstuff.txt","w") as outfile:
    for student in students:
        outputline = ""
        outputline += student['first']
        outputline += " "
        outputline += student['last']
        outputline += ": "
        outputline += ", ".join(student['grades'])
        # ", ".join(list) gives you a string with every element of list
        # separated by a comma and a space, e.g. ','.join(["1","2","3"]) == "1, 2, 3"
        outputline += "|| average: "
        average = str(sum(map(int,student['grades']))/len(student['grades']))
        # since student['grades'] is a list of strings, and we need to add them, you
        # have to use map(int, student['grades']) to get their int representations.
        # this is equivalent to [int(grade) for grade in student['grades']]
        outputline += average
        outputline += "
"

        outfile.write(outputline)

        # again, this can be done in one line
        # outfile.write("{0['first']} {0['last']}: {1}||{2}
".format(
        #              student, ', '.join(student['grades']), sum(map(int,student['grades']))/len(student['grades']))
        # but, again, this is long and unwieldy.
Comment

PREVIOUS NEXT
Code Example
Python :: Python-Specific Operators 
Python :: dataframe missing and zero values 
Python :: Like strings (and all other built-in sequence type), lists can be indexed and sliced: 
Python :: linkedin bot python 
Python :: parseint python equivalent 
Python :: import all csv as individual dataframes python 
Python :: ring for loop 
Python :: ring Conversion Number 
Python :: localizar la fila y columna de un dato pandas 
Python :: ring check if a Ring function is defined or not 
Python :: pandas rolling list 
Python :: open urls using python grepper 
Python :: mehrzeiliges kommentar python 
Python :: py3 identify file extension 
Python :: unpack list python 
Python :: how to add illegal characters to paths python 
Python :: range function in python use cases 
Python :: heatmap choos format for annotation 
Python :: python tuple multiply sequence 
Python :: removeStopWords 
Python :: ptyhton json respones 
Python :: File C:Users7shalPycharmProjectsItsa Me Malariomain.py, line 2 from pygame.locals import import ^ SyntaxError: invalid syntax 
Python :: python set prcess name 
Python :: first rows of data frame (specify n by param) 
Python :: python documentacion comentarios 
Python :: Select a Column in pandas data Frame Using dot notation 
Python :: 1046 uri solution 
Python :: pandas subtract two columns 
Python :: matplotlib facet scatter 
Python :: double linked list python 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =