Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 5

191 bytes removed, 16:43, 4 September 2017
PART 1 - Reading Data From Files
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 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 white-spacewhitespace, 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