So I wrote a python script that formats a text file so I can import into my SQL. I am using python 3.5 and my code works perfectly.
However, When I try to run my co
The short answer: use io.open
which has the same signature as python 3's open
.
The csv
module handles line ending for you so that it can deal with line endings different than local file system encodings. For instance, a dialect may want to write \r\n
line endings even on linux. In python 2, the solution was to open files in binary mode.
In python 3, things are different. Files opened in binary mode return bytes
objects that need decoding to become unicode string objects. You can open in text mode, but that does two things - decoding and newline translation. So the newline
keyword was invented. It lets you open in text mode for decoding but leaves the newline terminator in the string.
This functionality is also available in the io.open
function available on both python 2 and 3. You can use that function to get what you want. Notice that you also need to make some sort of a decoding decision. By default its whatever sys.getfilesystemencoding()
returns. You may need to make a decision about how you are encoding your csv files in the first place and use that encoding in your files.