I am working on a problem and trying to read a data and creating a chart the problem is when ever I read any thing from file it add \n or \r in with it but I change the type of digital data but I dont know what to do with string data like names. so far I am stuck here is my code.

class Student:

def __init__(self,Fname,Lname,Tid,CrdtEarn,QualityPnts):

self.Fname=Fname
self.Lname=Lname
self.Tid=Tid
self.CrdtEarn=CrdtEarn
self.QualityPnts=QualityPnts

## def __str__(self):
## return self.Fname+self.Lname+" is the student with Tech ID "+str(self.Tid)+" earned "+\
## str(self.CrdtEarn)+" and have "+str(self.QualityPnts)+" points."

def getFname(self):
return self.Fname

def getLname(self):
return self.Lname

def getTid(self):
return self.Tid

def getCrdtEarn(self):
return self.CrdtEarn

def getQualityPnts(self):
return self.QualityPnts

def Update(self,addHours,addQpoints):
self.CrdtEarn=CrdtEarn+addHours
self.QualityPnts=QualityPnts+addQpoints

ListF=[]
ListL=[]
ListT=[]
ListC=[]
ListQ=[]
def read():
inFile=open("prog3.txt","r")
flag=False
count=0
while not flag:

fName=inFile.readline()
if (fName != ""):
count+=1

lName=inFile.readline()
tid=inFile.readline()
crdt=inFile.readline()
points=inFile.readline()


tid=int(tid)
crdt=int(crdt)
points=int(points)

s=Student(fName,lName,tid,crdt,points)
## print List
ListF.append(s.Fname)
ListL.append(s.Lname)
ListT.append(s.Tid)
ListC.append(s.CrdtEarn)
ListQ.append(s.QualityPnts)
## print s
else:
flag=True
## print count

inFile.close()
return ListF,ListF,ListL,ListT,ListC,ListQ

def main():

read()
print ListF[0],ListL[0]
print ListL,"\n"
print ListT,"\n"
print ListC,"\n"
print ListQ,"\n"

main()

If anyone can help me I would be grateful
and this code is still incomplete

Please wrap your code in tags when posting it to this forum, so that it won't lose indentation, thereby allowing us to easily read your code:
[code=python] #Python code in here

[/code]

When reading from a file it is good practice to use strip() on every line to remove the line-endings (\n on unix and \r\n on windows). ie,

fh = open( 'myfile.txt' )
for line in fh:
    line = line.strip()
    # Do my line stuff now
fh.close()

In the above example I remove any trailing/leading whitespace (line endings included) before doing anything else.

HTH

ALWAYS use CODE tags when posting code. Also all of the leading whitespace is missing from your posted code. In python, leading whitespace is an essential part of your code, you can't compile without it.

(I was going to re-post your code here with the code tags, but I don't feel like taking the time to put all the whitespace back.)

If you have read from a text file and you find the strings have the \n at the end, you can remove it using a slice.

myline = infile.readline()
fname = myline[:-1]

The myline[:-1] will return all but the last character from myline.

You went to the trouble of writing the Student class, why are you replicating all of the data in the lists? (ListF, ListL, ListT, ListC, ListQ)

Why not add a 'display' or 'dump' method to the Student and then print that for debug?

I am sorry about not writing my code that way,

and thank you guys but I figured out the slicing so I did it thank you so much guys...

This article has been dead for over six months. Start a new discussion instead.