Python: Storing data in a text file and appending particular/individual lines -


this computer science project i've been working on. test scores saved in text file against students name. example:

rob 21 terry 12 mike 33 

i can program want read lines of text file , identify if name in existence. if so, should add next score onto end of line. if terry takes test again scores should read:

rob 21 terry 12 23 mike 33 

this relevant piece of code. starts after test complete , user has input name, class , received score.

import fileinput  print("well done " +name+ ", score "+ (str(score)))      entry = (name +" "+ (str(score)))       if classchoice == "a":          classfile = open("classa.txt")         line in classfile:             if name in line:                 oldline = line                 newline = (oldline+" "+(str(score)))                 print (newline)                 classfile.close()             else:                 classfile = open("classb.txt","a")                 classfile.write(entry)                 classfile.write('\n')                 classfile.close()          line in fileinput.input("classa.txt", inplace=1):             line = line.replace(oldline,newline)             line = line.strip() 

i'm having difficulty because:

  1. the first part reads lines in files , finds student name , results works when try put line new score ends putting new score underneath when print (newline) looks like:

    terry 12 23

  2. another issue else doesn't work. get: local variable 'oldline' referenced before assignment

could me this. i'm new python , little overwhelming @ moment.

this because when read file , , each line, has newline (\n) @ end, when -

newline = (oldline+" "+(str(score))) 

oldline has \n @ end. , hence - name oldscore\n newscoe , , hence comes on new line.

you need strip off previous newline before create newline, example -

newline = (oldline.rstrip()+" "+(str(score))) 

--

also, doing seems inefficient, can directly use fileinput.input() case -

if classchoice == "a":     write_flag = true     fileinput.input("classa.txt", inplace=1) f:         line in f:             line = line.rstrip()             if name in line:                 line = line + " " + str(score)                 write_flag = false             print(line)         #this if `name` never found, meaning have add name file score.     if write_flag:         open("classa.txt",'a') f:             f.write("{} {}\n".format(name,score)) 

as indicated in comments, using in result in wrong entries getting updated. way overcome split line , compare first entry in split -

if classchoice == "a":     write_flag = true     fileinput.input("classa.txt", inplace=1) f:         line in f:             line = line.rstrip()             words = line.split()             if name == words[0]:                 line = line + " " + str(score)                 write_flag = false             print(line)         #this if `name` never found, meaning have add name file score.     if write_flag:         open("classa.txt",'a') f:             f.write("{} {}\n".format(name,score)) 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -