python - Splitting lines in a text file into separate entries in a list? -
so, want code program allows user various tasks, 1 of having trouble with.
-option allows user search text file job's 'estimate id', , search file matching id , return relevant data in following format:
estimate number: (the estimate number)
customer id: (the customer id)
estimate amount: (the final total)
estimate date: (the estimate data)
status: (the status)
now, below format data stored in in text file:
e5346,23/09/2015,c107,970,n,0
e5347,23/09/2015,c108,1050,e,0
e5348,23/09/2015,c109,370,a,200
data shown estimate number, estimate date, customer id, final total (in £ pounds), status (e estimate, accepted job or n not accepted), , amount paid (in £ pounds). these in wrong order, , different entries on 1 line, separated commas, format cannot changed.
below code wrote this:
est_no = input ("please enter estimate number wish search: ") data = [] search = open("paintingjobs.txt","r") line in search: if (est_no) in line: data.append (line) print ("estimate number:",data[0]) print ("customer id:", data[2]) print ("estimate amount:",data[3]) print ("estimate date:",data[1]) if data[4] == "e": status = ("estimate") if data[4] == "a": status = ("accepted job") if data[4] == "n": status = ("not accepted") print ("status:",status) search.close()
i had thought great idea of putting line list, and, entries separated commas, independent entries in list, , print required, but, adds line file 1 entry; when print 1st entry in list, want estimate number, prints whole line, need split separate entries want. question is, how?
use string.split() split string delimiter (in case, delimiter ","
).
your code under if
block follows:
# remove data variable definition above loop if (est_no) in line: data = line.split(',') # process data list doing
Comments
Post a Comment