File I/O



File I/OWhy read in from a filereading could also be called “parsing” since we are looking at individual datacan you image typing all this crap in!!! (crap being data)ease of usefirst permanent storage for many of usthe data layout or format of the file is importantthe data files is usually givenso you can and MUST inspect it!!you and your program can create a data fileso you get to set the data layoutyou must know Strings and Lists well!!Strings to parseLists to store data (parsed data)Reading a filemuch like reading a text book, we read from top to bottom, left to rightcannot skip any dataEvery line is read in this (every line counts)data patterns (format) in a file play an important role for data retrievaleverything read in is treated as a string first!!then you cast to another data typesame as grabbing input from the keyboard!Typical data file layoutsV1V2V3A1 b1 c1 d1A2 b2 c2 d2A3 b3 c3 d3a – d are pieces of data separated by spacesA1B1C1A2B2C2D2…As part of your application, you need to complete a brief insurance physical - also known as a paramedical exam. A representative from APPS will contact you to schedule your paramedical exam. The exam will be conducted by a qualified medical professional and the results are strictly confidential. The examination will be completed at a date, time and place of your choice and at no cost to you.easy to parse since a patterneasy to parse since a patternnot so easy to parseExample of an Unusable data file layoutsV1A1 b1 c1 d1 e1A2 b2 c2A3 b3 c3 d3A4…bad because there is no pattern and it is inconsistentReading/Writing ThoughtsWhen reading in from a fileYOU ARE THE ONE WHO CREATED THE FILE!!!! So you know how it is laid out.Or the file will be given to you (like scores.txt)Since you are reading values IN, do NOT set any variables in your program!!! Since you are getting the value from the file4343400635PROGRAM(reads in values)00PROGRAM(reads in values)0635lastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstname00lastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstname297180010985500When writing OUT to a fileYOU ARE THE ONE WHO CREATING THE FILE!!!! So you design how it is laid out.Since you are writing values OUT, your program will set the values, then write!!!388620093980lastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstname00lastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstnamelastname firstname093980PROGRAM(write out values)00PROGRAM(write out values)24003008890000Visually inspecting the data file given (or created)whatever file is read in or created should be inspecteditems to inspectorder of dataeach piece has a specific purposedata types of each pieceremember we will have to castdata formathow is the data laid out?make sure the cursor lowest position is on the LAST lineInspecting good filesV1V2 notice the “cursor” is at the END of the last lineThe invisible markers on a file you can’t seebehind the scenes there are markers that help the file existbut these markers are sometimes read in with the data as well!!except EOFThe invisible file markersNewline \nEOF (End of File)Tab \twas not present in fileOpening a file (for input or output)always within a try/catchVarious ways to open a fileManual openingtry: infile = open(“data.txt”, "r")except FileNotFoundError: print("file was not found, try again")using user input/variablefilename = input("Please enter a file to open")try: infile = open(filename, "r")except FileNotFoundError: print("file was not found, try again")Opening a file for OUTPUToutfile = open("results.txt", "w")for line in dataFromFile: if (debug): print("writing the line: " , line) outfile.write(line)Notice “infile” “outfile” – so anything that I may have for infile, can be switched for an outfile(inputs from a file) infileinput (inputs from a keyboard)(outputs to a file) outfileprint (outputs to the monitor)Closing a file (for input or output)file MUST be closed when doneimportant for outfile since it saves AND closes the file at the same timecan reopen closed filescan have several files openedinfile.close( )outfile.close() What does token mean?Item separated by a space or \n (\r)Actual FileToken CountLupoli is da bomb!!Matt failed his midterm!!! Ha Ha!!!46Steps for reading from a fileIdentify types of variables you will be reading from that fileNext declare an uninitialized variable for each of themLupoli.txt23 41 a LuperThen open the file for INPUTCheck if the file was opened correctlyRead in data from fileDisplay gathered values for verificationClose fileMake sure input/output files are in the right placewe are about to do an exercise, it’s important where the files are located Avoiding Data file placement problemsParallel with Source... open("Lupoli.txt", ‘r’)Inside Source... open("./src/Lupoli.txt", ‘r’)Inside a package... open("./src/dataSet/scores.txt", ‘r’)But / could be tricky! Why?? Hint: C:\note.txtCreate the file example above called “Lupoli.txt”. Within the SAME directory create “fileIO.py” and use the steps above, (except #5), to NEARLY complete your first function to read in from this file. Your program SHOULD run, just not display any important information yet. Answerb:Learning to Read data by Examplesthe following examples show different ways of reading in data using the same fileLearning by ExampleCodeOutput# open a fileinfile = open("data.txt", "r")# read in first lineline = infile.readline()# print first line JUST to make sureif (debug): print("'" , line, "'")' 0 0 0 'Wait, what? Why is the second ' on a new line?# read in SECOND lineline = infile.readline().strip()# print SECOND line JUST to make sureif (debug): print("'", line, "'")' 1 34 1 '# read in THIRD line line = infile.readline().strip()# print THIRD line JUST to make sureif (debug): print("'", line, "'")' 13 13 13 '# this will read the REST of the file (what's left)for line in infile: line = line.strip() #since String is not mutable print("'", line, "'")' -1 0 0 '' x x x '' y y y '' z z z 'Finish your #5 now with the knowledge you have gained. Add another line to Lupoli.txt with made up data. Have your program print the entire line(s) that is read in from the Lupoli.txt file. Answerb:Using a loop to read data continuallyIF (big if) there aremultiple lines in your fileeach line contains a patternuse a loop!Example Loop Setup89986654973498# open a filefile = open("data.txt", "r")# already opened the filefor line in file: line = line.strip() #since String is not mutable listData.append(line)# make sure data in list is correctprint(listData)#notice that no splitting was necessary since only one piece of data per linePlacing read in data into a listsince we know lists so well, and can manipulate them, it’s easier to place everything we read into a listwe still need to parse that list in order to get our data we really needPlacing File IO data into a listnum1 = -1num2 = -1char1 = ''nickname1 = ""dataList = list()try: infile = open("data.txt", "r") #using a loop, readline already embedded!! for line in infile: dataList.append(line.strip()) infile.close()except FileNotFoundError: print("file was not found, try again")print(dataList)['23 41 a Lupoli', '24 67 b Lee']Add another line (of any data) into Lupoli.txt. Using your already established code, read the data and place the data from the file into a list!! Display that list to confirm. Reading techniques and String’s SplitThere is a marker that tracks where you JUST read from in the filemake sure YOU THE PROGRAMMER place a string like address at the END of a lineeasier to pull from stringScenario #123 41 a Luperint, int, char, string# open a filefile = open("Luper.txt", "r")# read in first lineline = file.readline()#split the line since it has valuable datadata = line.split()num1 = int(data[0])num2 = int(data[1])letter = data[2] # no conversionword = data[3] # no conversionprint(num1 , ":", num2 , ":", letter , ":", word)file.close()Scenario #2One two threeString, String, String# read in first lineline = file.readline()#split the line since it has valuable datadata = line.split()word1 = data[0] # no conversionword2 = data[1] # no conversionword3 = data[2] # no conversionScenario #3Lupoli Shawn 1600 Penn…String, String, String (LONG)…data = line.split()word1 = data[0] # no conversionword2 = data[1] # no conversionaddress = “” #need to establish what datatype address isaddress = address.join(data[2:])#remember : means restScenario #4I wish Lupoli was easierString (LONG)line = file.readline()# DON’T TOKENIZE THE STRING!!! Scenario #523 Lupoli 45 BledsoeInt, String, Int, Stringnum1 = int(data[0])word1 = data[1] # no conversionnum2 = int(data[2])word2 = data[3] # no conversion(getting it all together video)Scenario #6Lohan Lindsay 23 1982Spears Brittney 10 2007You completeScenario #7Lupoli A A D C You completeWorking Examplein this simple example, we explore the different parts of the program togetherthe files will be given to youWorking Application – Print the file to screenfileIOExample.pydata.txt434467026035Last00Last# Part 1infile = open("data.txt", "r")# Part 2 - Read the data with a for loopfor line in infile: line = line.strip() #since String is not mutable print("'", line, "'") #Part 3 getting the data we need from each line #split the line since it has valuable data data = line.split()#Part 6 close the fileinfile.close()# please note, no try/except only to save space!!!46990026035First00FirstPrivitt Carl 68.9 1000Rios Ennis 87.9 100Styles Carrie 99 0Styles John 34 0Darlington Kris 45 10000000147891513970$ owe me $00$ owe me $Ducar Dallas 12 2187661252Walthers Jessica 96 0167005-5080Average00AverageKalkus Emily 91 150Collecting some data within a filein this application, we are ONLY collecting the averages to see what the overall class average isstepsidentify which column is needed for this datahow do we collecthow do we calculatewhen do we print a resultSelecting which parts of the data we wantNeed item #2place into a list that item AFTER each LINE is readuse mean list function (there are other ways)print result at endWorking Application – Collecting the averagesfileIOExampleV2.pyfrom statistics import mean #3.3 and above# Part 1infile = open("data.txt", "r")# Part 2#store data coming inaverages = list()# Part 3 - Read the data with a for loopfor line in infile: line = line.strip() #Part 4 getting the data we need from each line #split the line since it has valuable data data = line.split() averages.append(float(data[2]))#Part 6 close the fileinfile.close()print(mean(averages)) #orprint(sum(averages)/len(averages))# please note, no try/except only to save space!!!Why did I have to “float” the data?Why did I use a list?Why would printing the result in the middle of the loop be useless?Download these files into the SAME directory and run.Using the files you downloaded, edit fileIOExample.py to determine the TOTAL amount owed to me!!use above to helpvery few changes to above to get this to work!!Answerb:Adding to your code, print ONLY those that OWE me money!!will need an if statementremember, when pulling data from a file, what data type is it?Answerb:Writing to a fileSyntax – almost same as a displaying to a monitor, just a file insteadoutfile.write( … ) # without it, the data will be printed on one lineoutfile.write( x + “ “ + y + “\n”) # you can also put multiple variables in one lineWriting different types of datawriting a hard coded letter/numberwriting a variable letteroutfile.write(“f\n”)outfile.write(“2\n”)char quit = ‘q’outfile.write(quit)writing a hard coded sentencewriting a variable intoutfile.write( “Hello Class\n”)age = 23outfile.write(str(age))writing a stringString name = “Mr. Lupoli”outfile.write(name+”\n”) # space or no space okCombining it all togetheroutfile.write(“hello class, “ + name + “ “ + age + “ “ + quit + “\n”)Steps for writing to a fileIdentify types of variables you will be writing OUT to that file, unless you want to hard code an outputIdentify the ORDER you wish to place the data to the fileNext declare an initialized variable for each of themThen open the file for OUTPUTWrite dataClose fileAdding to your code write those that owe me money to a file named “deadbeats.txt”. But…. I want the file to look like below!you do NOT need to create a document!!Why??will need to look back on how to open a file for OUTPUTyou will need to close the output fileMore complex applicationsremember that loops will most likely be usedbut remember the rest of your trainingWhat does this application accomplish? #1def main(): infile = open("grades.txt", "r") scores = 0 count = 0 for line in infile: try: scores += float(line.strip()) count += 1 except ValueError: print("stuff") print("The total scores was", scores) print("The number of lines was", count) infile.close() outfile = open("results.txt", "w") outfile.write(str(scores/count)) outfile.close()main()What does this application accomplish?Create a sample data file that would work with this.What does this application accomplish? #2def printRegularTextFile(): flag = True while(flag): # must enter file they want to open try: infile = input("Please enter the name of the file you wish to open: ") infile = open(infile, "r") for line in infile: print(line.strip()) infile.close() flag = False except IOError: #(File IO error) print("invalid filename, please try again")What does this application accomplish?What NOT to do with File IORead ALL patterned data in ONE pass in a loopused a while loopbut read ALL data in ONE iteration of the loop, so didn’t use the loopNo No #1Using a for loop to read datafor i to 9: # data file had 9 values in it!!num = int(infile.readline())total += num# how many values will this read?Corrected Version# Create the reading loop using a while loop that will reads UNLIMITED valuesI have NOT received payment. I charge 13% interest. Using the data.txt file, update the file to look exactly the same BUT the amount owed is now 13% more. Write the results into “result.txt”. (Results.txt just to test, but it would make sense to overwrite data.txt)BeforeAfterPrivitt Carl 68.9 1000Rios Ennis 87.9 100Styles Carrie 99 0Styles John 34 0Darlington Kris 45 10000000Ducar Dallas 12 2187661252Walthers Jessica 96 0Kalkus Emily 91 150Privitt Carl 68.9 1130Rios Ennis 87.9 113Styles Carrie 99 0Styles John 34 0Darlington Kris 45 11300000Ducar Dallas 12 2472057214.76Walthers Jessica 96 0Kalkus Emily 91 169.5AnswersAll but #5dataList = list()num1 = -1num2 = -1letter1 = ""string1 = ""try: infile = open("Lupoli.txt", "r") #using a loop, readline already embedded!! for line in infile: dataList.append(line.strip()) infile.close()except FileNotFoundError: print("file was not found, try again")print(dataList)For Part 5.? Including #5 from a file data = “”try: infile = open("data.txt", "r") #using a loop, readline already embedded!! data = infile.readline.strip() infile.close()except FileNotFoundError: print("file was not found, try again")print(data)Reading numbers from a file & placing into a listdef exerice2():? ? dataList = list()? ??? ? try:? ? ? ? ? ? infile = open("numbers.txt", "r")? ? ? ? ? ? #using a loop, readline already embedded!!? ? ? ? ? ? for line in infile:? ? ? ? ? ? ? ? dataList.append(int(line.strip()))? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? infile.close()? ? except FileNotFoundError:? ? ? ? ? ? print("file was not found, try again")? ? return dataListReading from a file and placing into a listnickname1 = ""dataList = list()try: infile = open("data.txt", "r") #using a loop, readline already embedded!! for line in infile: dataList.append(line.strip()) infile.close()except FileNotFoundError: print("file was not found, try again")print(dataList)How much money do you owe me!!!# Part 1infile = open("data.txt", "r")bonesOwed = list()# Part 2 - Read the data with a for loopfor line in infile: line = line.strip() #Part 3 getting the data we need from each line #split the line since it has valuable data data = line.split() bonesOwed.append(float(data[3])) # no conversion#Part 6 close the fileinfile.close()print(sum(bonesOwed))Who owes me money?# Part 1infile = open("data.txt", "r")# Part 2 - Read the data with a for loopfor line in infile: line = line.strip() #Part 3 getting the data we need from each line #split the line since it has valuable data data = line.split() if(int(data[3]) > 0): print(data[0] + " " + data[1] + " owes me BONES!!")#Part 6 close the fileinfile.close()Deadbeats# Part 1infile = open("data.txt", "r")outfile = open("deadbeats.txt", "w")# Part 2 - Read the data with a for loopfor line in infile: line = line.strip() #Part 3 getting the data we need from each line #split the line since it has valuable data data = line.split() if(int(data[3]) > 0): print(data[0] + " " + data[1] + " owes me BONES!!") outfile.write(data[1] + " " + data[0] + " " + data[3] +"\n")#Part 6 close the fileinfile.close()outfile.close()Writing data from a list to a FileVersion 1num1 = -1num2 = -1char1 = ''nickname1 = ""dataList = list()try: infile = open("data.txt", "r") #using a loop, readline already embbeded!! for line in infile: dataList.append(line.strip()) infile.close()except FileNotFoundError: print("file was not found, try again")print(dataList)try: outfile = open("results.txt", "w") for element in dataList: outfile.write(element) outfile.close()except FileNotFoundError: print("file was not found, try again")Version 2…try: outfile = open("results.txt", "w") for element in dataList: outfile.write(element + "\n") outfile.close()except FileNotFoundError: print("file was not found, try again")Version 3…try: outfile = open("results.txt", "w") for element in dataList: outfile.write(element + "\n") # outfile.close()except FileNotFoundError: print("file was not found, try again")Parsing data in a file#6 (Lindsay Lohan 23 1982)lastName = data[0] # no conversionfirstName = data[1] # no conversionage = int(data[2])yearBorn = int(data[3])#7lastName = data[0] # no conversiongrade1 = data[1] # no conversiongrade2 = data[2] # no conversiongrade3 = data[3] # no conversiongrade4 = data[4] # no conversion ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download