line-endings

How to check what lineending a StringIO file is using?

How to check what lineending a StringIO file is using? Question: I had a method that detects line endings def getLineEnding(filename): ret = "rn" with open(filename, ‘r’) as f: f.readline() ret = f.newlines return ret In order to be able to test it without using real files, I changed it to: def getLineEnding(filehandle): filehandle.readline() return …

Total answers: 1

disable the automatic change from rn to n in python

disable the automatic change from rn to n in python Question: I am working under ubuntu on a python3.4 script where I take in parameter a file (encoded to UTF-8), generated under Windows. I have to go through the file line by line (separated by rn) knowing that the "lines" contain some ‘n’ that I …

Total answers: 2

Does Python csv writer always use DOS end-of-line characters?

Does Python csv writer always use DOS end-of-line characters? Question: I realize that the csv library in Python always generates DOS end-of-line characters. Even if I use the ‘wb’ mode, even if I use Linux. import csv f = open(‘output.txt’, ‘wb’); writer = csv.writer(f) writer.writerow([2,3,4]); f.close() The above code always uses ‘rn’ as the end …

Total answers: 2

Python 2 CSV writer produces wrong line terminator on Windows

Python 2 CSV writer produces wrong line terminator on Windows Question: According to the its documentation csv.writer should use ‘rn’ as lineterminator by default. import csv with open(“test.csv”, “w”) as f: writer = csv.writer(f) rows = [(0,1,2,3,4), (-0,-1,-2,-3,-4), (“a”,”b”,”c”,”d”,”e”), (“A”,”B”,”C”,”D”,”E”)] print writer.dialect.lineterminator.replace(“r”, “\r”).replace(“n”, “\n”) writer.writerows(rows) print writer.dialect.lineterminator.replace(“r”, “\r”).replace(“n”, “\n”) This prints rn rn as expected. …

Total answers: 3

What's a quick one-liner to remove empty lines from a python string?

What's a quick one-liner to remove empty lines from a python string? Question: I have some code in a python string that contains extraneous empty lines. I would like to remove all empty lines from the string. What’s the most pythonic way to do this? Note: I’m not looking for a general code re-formatter, just …

Total answers: 13

Python get proper line ending

Python get proper line ending Question: Is there an easy way to get the type of line ending that the current operating system uses? Asked By: Evan Fosmark || Source Answers: Oh, I figured it out. Apparently, PEP-278 states the following: Any line ending in the input file will be seen as a ‘n’ in …

Total answers: 4