# Create toy text file("CR")
write.table(mtcars, file = "toy_text.TXT",
col.names = FALSE,row.names = FALSE,
quote=FALSE, eol = "\r")
When I execute the above code, it generates a text file with "CR" because of the eol = "\r"
.
I am trying to convert CR to LF on a windows machine. Tweaking the solution given on StackOverflow
How to convert CRLF to LF on a Windows machine in Python - Stack Overflow, the Python code shown below works for me. If I understand correctly, the code simply replaces \r
with \n
in binary mode.
How do I achieve the same result using R?
# replacement strings
WINDOWS_LINE_ENDING = b'\r' # CR
UNIX_LINE_ENDING = b'\n' # LF
# relative or absolute file path, e.g.:
file_path = "toy_text.txt"
with open(file_path, 'rb') as open_file:
content = open_file.read()
# Windows ➡ Unix
content = content.replace(WINDOWS_LINE_ENDING, UNIX_LINE_ENDING)
with open(file_path, 'wb') as open_file:
open_file.write(content)