After looking into my question here, I found that it was caused by a simpler problem.
When I write "\n"
to a file, I expect to read in "\n"
from the file. This is not always the case in Windows.
In [1]: with open("out", "w") as file:
...: file.write("\n")
...:
In [2]: with open("out", "r") as file:
...: s = file.read()
...:
In [3]: s # I expect "\n" and I get it
Out[3]: '\n'
In [4]: with open("out", "rb") as file:
...: b = file.read()
...:
In [5]: b # I expect b"\n"... Uh-oh
Out[5]: b'\r\n'
In [6]: with open("out", "wb") as file:
...: file.write(b"\n")
...:
In [7]: with open("out", "r") as file:
...: s = file.read()
...:
In [8]: s # I expect "\n" and I get it
Out[8]: '\n'
In [9]: with open("out", "rb") as file:
...: b = file.read()
...:
In [10]: b # I expect b"\n" and I get it
Out[10]: b'\n'
In a more organized way:
| Method of Writing | Method of Reading | "\n" Turns Into |
|-------------------|-------------------|-----------------|
| "w" | "r" | "\n" |
| "w" | "rb" | b"\r\n" |
| "wb" | "r" | "\n" |
| "wb" | "rb" | b"\n" |
When I try this on my Linux virtual machine, it always returns \n. How can I do this in Windows?
Edit:
This is especially problematic with the pandas library, which appears to write DataFrame
s to csv
with "w"
and read csv
s with "rb"
. See the question linked at the top for an example of this.