<p dir="auto"><em>Method 1:</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="        data_frame.to_csv(file_path, sep=self.delimiter, float_format='%.2f',
                                  index=False, line_terminator='\n')
"><pre class="notranslate"><code class="notranslate">        data_frame.to_csv(file_path, sep=self.delimiter, float_format='%.2f',
                                  index=False, line_terminator='\n')

</code></pre></div>
<p dir="auto"><em>Method 2:</em></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="        with open(file_path, mode='w', newline='\n') as f:
            data_frame.to_csv(f, sep=self.delimiter, float_format='%.2f',
                              index=False)"><pre class="notranslate"><code class="notranslate">        with open(file_path, mode='w', newline='\n') as f:
            data_frame.to_csv(f, sep=self.delimiter, float_format='%.2f',
                              index=False)
</code></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">I noticed a strange behavior when using <em>pandas.DataFrame.to_csv</em> method on Windows (pandas version 0.20.3). When calling the method using <em>method 1</em> with a file path, it's creating a new file using the \r line terminator,  I had to use method two to make it work.</p>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def hex_print(content):
    print ' '.join(['{0:02x}'.format(ord(i)) for i in content])
    print ' '.join(['{:&gt;2}'.format(repr(i).replace(&quot;'&quot;, '')) for i in content])
    print ' '

import pandas as pd
import tempfile

filename = tempfile.NamedTemporaryFile(delete = False).name

df = pd.DataFrame({'x':[1]})
for sep in ['\n', '\r', '\r\n', 'F']:
    print 'with separator: {} ~~~~~~~~~~~~~~~~~~~~~~~~'.format(repr(sep))
    df.to_csv(filename, line_terminator = sep)        
    with open(filename, 'rb') as f:
        content = f.read()
    print 'file method:'
    hex_print(content)
    print 'string method:'
    hex_print(df.to_csv(line_terminator = sep))
"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">hex_print</span>(<span class="pl-s1">content</span>):
    <span class="pl-k">print</span> <span class="pl-s">' '</span>.<span class="pl-en">join</span>([<span class="pl-s">'{0:02x}'</span>.<span class="pl-en">format</span>(<span class="pl-en">ord</span>(<span class="pl-s1">i</span>)) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">content</span>])
    <span class="pl-k">print</span> <span class="pl-s">' '</span>.<span class="pl-en">join</span>([<span class="pl-s">'{:&gt;2}'</span>.<span class="pl-en">format</span>(<span class="pl-en">repr</span>(<span class="pl-s1">i</span>).<span class="pl-en">replace</span>(<span class="pl-s">"'"</span>, <span class="pl-s">''</span>)) <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-s1">content</span>])
    <span class="pl-k">print</span> <span class="pl-s">' '</span>

<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">tempfile</span>

<span class="pl-s1">filename</span> <span class="pl-c1">=</span> <span class="pl-s1">tempfile</span>.<span class="pl-v">NamedTemporaryFile</span>(<span class="pl-s1">delete</span> <span class="pl-c1">=</span> <span class="pl-c1">False</span>).<span class="pl-s1">name</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>:[<span class="pl-c1">1</span>]})
<span class="pl-k">for</span> <span class="pl-s1">sep</span> <span class="pl-c1">in</span> [<span class="pl-s">'<span class="pl-cce">\n</span>'</span>, <span class="pl-s">'<span class="pl-cce">\r</span>'</span>, <span class="pl-s">'<span class="pl-cce">\r</span><span class="pl-cce">\n</span>'</span>, <span class="pl-s">'F'</span>]:
    <span class="pl-k">print</span> <span class="pl-s">'with separator: {} ~~~~~~~~~~~~~~~~~~~~~~~~'</span>.<span class="pl-en">format</span>(<span class="pl-en">repr</span>(<span class="pl-s1">sep</span>))
    <span class="pl-s1">df</span>.<span class="pl-en">to_csv</span>(<span class="pl-s1">filename</span>, <span class="pl-s1">line_terminator</span> <span class="pl-c1">=</span> <span class="pl-s1">sep</span>)        
    <span class="pl-k">with</span> <span class="pl-en">open</span>(<span class="pl-s1">filename</span>, <span class="pl-s">'rb'</span>) <span class="pl-k">as</span> <span class="pl-s1">f</span>:
        <span class="pl-s1">content</span> <span class="pl-c1">=</span> <span class="pl-s1">f</span>.<span class="pl-en">read</span>()
    <span class="pl-k">print</span> <span class="pl-s">'file method:'</span>
    <span class="pl-en">hex_print</span>(<span class="pl-s1">content</span>)
    <span class="pl-k">print</span> <span class="pl-s">'string method:'</span>
    <span class="pl-en">hex_print</span>(<span class="pl-s1">df</span>.<span class="pl-en">to_csv</span>(<span class="pl-s1">line_terminator</span> <span class="pl-c1">=</span> <span class="pl-s1">sep</span>))</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">It seems that the <code class="notranslate">to_csv</code> does not always handle the <code class="notranslate">line_terminator</code> argument correctly.  The above code prints out the hexified CSV data produced from several different calls to <code class="notranslate">to_csv</code>.  In particular, passing <code class="notranslate">\n</code> in fact produces <code class="notranslate">\r\n</code>, and <code class="notranslate">\r\n</code> becomes <code class="notranslate">\r\r\n</code>.  Note also that this only happens when writing to a file, not directly returning the CSV data as a string.</p>
<p dir="auto">However, this seems to be OS-dependent as well -- I have reproduced it on several machines running Windows 7, Python 2.7, and various versions of pandas (including 0.20.1), but on a linux VM, it works as expected.</p>
<p dir="auto">Output of above code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="with separator: '\n' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 0a 30 2c 31 0d 0a
 ,  x \r \n  0  ,  1 \r \n

string method:
2c 78 0a 30 2c 31 0a
 ,  x \n  0  ,  1 \n

with separator: '\r' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 30 2c 31 0d
 ,  x \r  0  ,  1 \r

string method:
2c 78 0d 30 2c 31 0d
 ,  x \r  0  ,  1 \r

with separator: '\r\n' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 0d 0a 30 2c 31 0d 0d 0a
 ,  x \r \r \n  0  ,  1 \r \r \n

string method:
2c 78 0d 0a 30 2c 31 0d 0a
 ,  x \r \n  0  ,  1 \r \n

with separator: 'F' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 46 30 2c 31 46
 ,  x  F  0  ,  1  F

string method:
2c 78 46 30 2c 31 46
 ,  x  F  0  ,  1  F
"><pre class="notranslate"><code class="notranslate">with separator: '\n' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 0a 30 2c 31 0d 0a
 ,  x \r \n  0  ,  1 \r \n

string method:
2c 78 0a 30 2c 31 0a
 ,  x \n  0  ,  1 \n

with separator: '\r' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 30 2c 31 0d
 ,  x \r  0  ,  1 \r

string method:
2c 78 0d 30 2c 31 0d
 ,  x \r  0  ,  1 \r

with separator: '\r\n' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 0d 0d 0a 30 2c 31 0d 0d 0a
 ,  x \r \r \n  0  ,  1 \r \r \n

string method:
2c 78 0d 0a 30 2c 31 0d 0a
 ,  x \r \n  0  ,  1 \r \n

with separator: 'F' ~~~~~~~~~~~~~~~~~~~~~~~~
file method:
2c 78 46 30 2c 31 46
 ,  x  F  0  ,  1  F

string method:
2c 78 46 30 2c 31 46
 ,  x  F  0  ,  1  F

</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 2.7.13.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.20.1<br>
pytest: None<br>
pip: 9.0.1<br>
setuptools: 35.0.1<br>
Cython: None<br>
numpy: 1.13.1<br>
scipy: None<br>
xarray: None<br>
IPython: 5.3.0<br>
sphinx: 1.5.5<br>
patsy: None<br>
dateutil: 2.6.1<br>
pytz: 2017.2<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
feather: None<br>
matplotlib: 2.0.0<br>
openpyxl: None<br>
xlrd: 1.0.0<br>
xlwt: None<br>
xlsxwriter: 0.9.6<br>
lxml: None<br>
bs4: 4.5.3<br>
html5lib: 0.999999999<br>
sqlalchemy: 1.1.7<br>
pymysql: None<br>
psycopg2: 2.7.1 (dt dec pq3 ext lo64)<br>
jinja2: 2.9.6<br>
s3fs: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>