Changes

Jump to: navigation, search

OPS435 Python Lab 5

177 bytes added, 17:27, 4 September 2017
PART 1 - Reading Data From Files
list_of_lines = read_data.split('\n') # Saves returned list in variable
list_of_lines
</source>:#The <br>Although the above sequence works, but itthere are '''functions''' and '''methods'''s not the best way we can use with '''our object (called "f")''' to get place lines from our file into a '''list of lines'''. The main benefit above This would help to reduce code and is that the considered a more common method to store multiple lines can be split in any way desired(on spaces, on periods, etc)or records within a list. The easiest way <br><br>:#Issue both method1 and method2 sequence of commands to just get see how you can store data into a list of all lines can be done using the file object and it's methods.more efficiently:<source lang="python"># METHOD 1:
f = open('data.txt', 'r')
method1 = list(f)
f.close()
method1
 
# METHOD 2:
 
f = open('data.txt', 'r')
method2 = f.readlines()
13,420
edits

Navigation menu