Changes

Jump to: navigation, search

OPS435 Python Lab 5

332 bytes removed, 23:10, 10 September 2017
PART 1 - Reading Data From Files
read_data
</source><br>After you have completed accessing data within a file, you should '''close''' the file in order to free up the computer resources. It is sometimes useful to first confirm that the file is still open prior to closing it. But really you should know - it's your code that would have opened it.<br><br>
:#Issue Look at the following object attribute will provide (it is a boolean value ) to confirm that the file is still open (''true'' indicates closed and ''false'' indicates open):<source lang="python">
f.closed
</source>
:#Finally, issue the following object method and object attribute to close the filewith the close() method, and then verify that the file has been successfully closed:<source lang="python">
f.close() # This method will close the file
f.closed # Confirm that the file is closed
read_data
</source><br>This command displays the data from the file in a single <u>long</u> string. The end of each line in the file will show the special character ''''\n'''' which represents the '''newline character''' in a file used to separate lines (or records in a traditional "flat database file"). It would be convenient to '''split''' the line on the new-line characters, so each line can be stored into a separate array elements (or in our case, a list!).<br><br>
:#First issue the following commands to get help on the '''split''' method:<source lang="python">
dir(read_data)
help(read_data.split)
</source>
:#Next, issue the following commands to store the contents of our file into a list called '''list_of_lines''':<source lang="python">
read_data.split('\n') # Returns a list
list_of_lines = read_data.split('\n') # Saves returned list in variable
list_of_lines
</source><br>Although the above sequence works, there are '''functions''' and '''methods''' the we can use with '''our object (called "f")''' to place lines from our file into a '''list'''. This would help to reduce code and is considered a more common method to store multiple lines or records within a list.<br><br>
:#Issue both method1 and method2 sequence of commands Try these two different means to see how you can store data into a list more efficiently:<source lang="python">
# METHOD 1:
f.close()
method2
</source><br>Sometimes, you may need Here's another way to only display retrieved data on the screen as opposed to processing the dataread a file. In these simple cases, there is no need to create extra-large variables to store the data, but rather print this case you're reading one line at a time onto rather than the screen. Using this method will save computer resources while reading filesentire file at once.The result looks the same since you're printing each line after the other, but you could potentially perform some operations on each line separately instead of just printing them:<br><br>:#To demonstrate, issue the following code sequence:<source lang="python">
f = open('data.txt', 'r')
for line in f:
print(line, end='')
f.close()
</source><br>We can improve upon the previous coding sequence by using the '''strip()''' function. In the previous coding example, the The python ''print()'' function by default adds the new-line character to the end of the line. Using the '''end='''' argument used in with the print () function replaces the '\n' at the end with nothing ''. This allows But in this example the print() to use lines we're printing already have newline characters in the new-line characters found on each line of end so we're getting exactly the file that was readsame output as we had input. Though (if desired) Alternatively you can always strip the new-line characters from any lines. The strip() function will remove all leading and trailing white-space, which may help in processing some lines or types of text data.<br>:<br>:#To demonstrate, issue the following code sequence:<source lang="python">
f = open('data.txt', 'r')
for line in f:

Navigation menu