I can\'t figure out why the code #1 returns an extra empty line while code #2 doesn\'t. Could somebody explain this? The difference is an extra comma at the end of the code
There is a very simple fix for this.
Let's say I have, for arguments sake, a .txt file (called numbers.txt) which I am reading from with the values:
234234
777776
768768
Then, using the following code:
filename = numbers.txt
with open(filename) as file_object:
for line in file_object:
print(line)
Would give you the extra blank line as you are having problems with. But with a small change, you can get rid of the extra blank lines using the rstrip() method. So the code becomes:
filename = numbers.txt
with open(filename) as file_object:
for line in file_object:
print(line.rstrip())
Now you will get your results without those annoying blank lines between them!