Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stdlib csv module to write csv #206

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# pylint: disable=W0212,W0231,W0703,W0622

from StringIO import StringIO
import csv
import operator
import warnings

Expand Down Expand Up @@ -485,13 +486,13 @@ def to_csv(self, path, nanRep='', cols=None, header=True,
mode : Python write mode, default 'wb'
"""
f = open(path, mode)
csvout = csv.writer(f)

if cols is None:
cols = self.columns

series = self._series
if header:
joined_cols = ','.join([str(c) for c in cols])
if index:
# should write something for index label
if index_label is None:
Expand All @@ -509,31 +510,26 @@ def to_csv(self, path, nanRep='', cols=None, header=True,
elif not isinstance(index_label, (list, tuple, np.ndarray)):
# given a string for a DF with Index
index_label = [index_label]
f.write('%s,%s' % (",".join(index_label), joined_cols))
csvout.writerow(list(index_label) + list(cols))
else:
f.write(joined_cols)
f.write('\n')
csvout.writerow(cols)

nlevels = getattr(self.index, 'nlevels', 1)
for idx in self.index:
row_fields = []
if index:
if nlevels == 1:
f.write(str(idx))
row_fields = [idx]
else: # handle MultiIndex
f.write(",".join([str(i) for i in idx]))
row_fields = list(idx)
for i, col in enumerate(cols):
val = series[col].get(idx)
if isnull(val):
val = nanRep
else:
val = str(val)

if i > 0 or index:
f.write(',%s' % val)
else:
f.write('%s' % val)
row_fields.append(val)

f.write('\n')
csvout.writerow(row_fields)

f.close()

Expand Down
5 changes: 3 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pylint: disable=E1101,E1103
# pylint: disable=W0703,W0622,W0613,W0201

import csv
import itertools
import operator
import sys
Expand Down Expand Up @@ -1589,8 +1590,8 @@ def to_csv(self, path):
Output filepath. If None, write to stdout
"""
f = open(path, 'wb')
for idx, value in self.iteritems():
f.write(str(idx) + ',' + str(value) + '\n')
csvout = csv.writer(f)
csvout.writerows(self.iteritems())
f.close()

def dropna(self):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,16 @@ def test_to_csv_float32_nanrep(self):
lines = open(pth).readlines()
self.assert_(lines[1].split(',')[2] == '999')
os.remove(pth)

def test_to_csv_withcommas(self):
"Commas inside fields should be correctly escaped when saving as CSV."
path = '__tmp__'
df = DataFrame({'A':[1,2,3], 'B':['5,6','7,8','9,0']})
df.to_csv(path)
df2 = DataFrame.from_csv(path)
assert_frame_equal(df2, df)

os.remove(path)

def test_info(self):
io = StringIO()
Expand Down