Changes

Jump to: navigation, search

OPS435 Python Lab 5

170 bytes added, 17:43, 4 September 2017
PART 1 - Reading Data From Files
f.close()
method2
</source><br>Sometimes, you may need to only display retrieved data on the screen as opposed to processing the data. In these simple cases, there is no need to create extra-large variables to store the data, but rather print one line at a time onto the screen. Using this method will save computer resources while reading files.<source lang="python"><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>:#The <br>We can improve upon the previous coding sequence by using the '''strip()''' function. In the previous coding example, the python ''print() '' function by default adds the new-line character to the end of the line. Using the ''end='''' argument used in print replaces the '\n' at the end with nothing ''. This allows the print() to use the new-line characters found on each line of the file that was read. Though (if desired ) you can always strip the new-line characters from any lines. The strip() function will remove all leading and trailing whitespacewhite-space, which may help in processing some lines or data.<br><br>:#To demonstrate, issue the following code sequence:<source lang="python">
f = open('data.txt', 'r')
for line in f:
13,420
edits

Navigation menu