Question was answered and worked.
Asked
Active
Viewed 197 times
1
-
Can you post the file you are reading – Ishan Joshi Sep 27 '19 at 09:17
-
yes sure. It is a text file by the name of Earth.txt inside it is this: aaaabbdffee ccrrffddgfdss – Sep 27 '19 at 09:20
2 Answers
1
The file.readline method always returns a string with a trailing newline character (unless it's at the end of file and the file does not end with a newline character), so you should use the str.rstrip method to remove the trailing newline character before processing the string:
line1 = f.readline().rstrip('\n')
blhsing
- 77,832
- 6
- 59
- 90
-
2
-
I've updated the code so that it will only remove trailing newline characters then. – blhsing Sep 27 '19 at 09:25
0
f.readline()
Reads each line in the file, including empty lines, for which it will just return the linebacker ('\n'). Usually you don't see it because print() handles it automatically, but your function counts this as a separate character. See if you have any empty lines at the end of your file, and just backspace them and save. Hope this helps.
Petru Tanas
- 861
- 1
- 9
- 31