The problem is that iterating over a file leaves the newline characters on the end:
>>> for i in open("a.py"):
... print(repr(i))
...
'import math\n'
'\n'
'\n'
<snipped>
Therefore, when you print the lines, the print function adds another "\n" to the end, and you get something similar to:
>>> print("import math\n")
import math
>>>
The easiest way to remedy this (I am assuming you are using Python 3, judging by the brackets with the print) is to use the end option for print:
for eachline in sort:
print(eachline, end="")
This will stop print from adding an extra "\n" (it adds "" instead, which does nothing), and so there are no extra newlines:
>>> print("import math\n", end="")
import math
>>>
If you are on python 2 or don't want to modify the print, you can strip the "\n" using str.replace:
for eachline in sort:
print(eachline.replace("\n", ""))
operator.itemgetter(0)will sort by the first character of each line. Are you sure you weren't trying to sort by the first column of the CSV?