Skip to content

ENH: pd.read_clipboard detects tab-separated data (excel) GH6223 #6226

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

Merged
1 commit merged into from Feb 4, 2014
Merged
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
4 changes: 4 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ New features
API Changes
~~~~~~~~~~~


Experimental Features
~~~~~~~~~~~~~~~~~~~~~

Improvements to existing features
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- pd.read_clipboard will, if 'sep' is unspecified, try to detect data copied from a spreadsheet
and parse accordingly. (:issue:`6223`)

.. _release.bug_fixes-0.14.0:

Bug Fixes
Expand Down
5 changes: 4 additions & 1 deletion doc/source/v0.14.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ There are no deprecations of prior behavior in 0.14.0
Enhancements
~~~~~~~~~~~~

- pd.read_clipboard will, if 'sep' is unspecified, try to detect data copied from a spreadsheet
and parse accordingly. (:issue:`6223`)


Performance
~~~~~~~~~~~


Experimental
~~~~~~~~~~~~

Expand Down
21 changes: 19 additions & 2 deletions pandas/io/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ def read_clipboard(**kwargs): # pragma: no cover
-------
parsed : DataFrame
"""
if kwargs.get('sep') is None and kwargs.get('delim_whitespace') is None:
kwargs['sep'] = '\s+'
from pandas.util.clipboard import clipboard_get
from pandas.io.parsers import read_table
text = clipboard_get()

# Excel copies into clipboard with \t seperation
# inspect no more then the 10 first lines, if they
# all contain an equal number (>0) of tabs, infer
# that this came from excel and set 'sep' accordingly
lines = text[:10000].split('\n')[:-1][:10]

# Need to remove leading white space, since read_table
# accepts:
# a b
# 0 1 2
# 1 3 4

counts = set([x.lstrip().count('\t') for x in lines])
if len(lines)>1 and len(counts) == 1 and counts.pop() != 0:
kwargs['sep'] = '\t'

if kwargs.get('sep') is None and kwargs.get('delim_whitespace') is None:
kwargs['sep'] = '\s+'

# try to decode (if needed on PY3)
if compat.PY3:
try:
Expand Down
35 changes: 35 additions & 0 deletions pandas/io/tests/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy.random import randint

import nose
import pandas as pd

from pandas import DataFrame
from pandas import read_clipboard
Expand Down Expand Up @@ -65,3 +66,37 @@ def test_round_trip_frame_string(self):
def test_round_trip_frame(self):
for dt in self.data_types:
self.check_round_trip_frame(dt)

def test_read_clipboard_infer_excel(self):
from textwrap import dedent
from pandas.util.clipboard import clipboard_set

text = dedent("""
John James Charlie Mingus
1 2
4 Harry Carney
""".strip())
clipboard_set(text)
df = pd.read_clipboard()

# excel data is parsed correctly
self.assertEqual(df.iloc[1][1], 'Harry Carney')

# having diff tab counts doesn't trigger it
text = dedent("""
a\t b
1 2
3 4
""".strip())
clipboard_set(text)
res = pd.read_clipboard()

text = dedent("""
a b
1 2
3 4
""".strip())
clipboard_set(text)
exp = pd.read_clipboard()

tm.assert_frame_equal(res, exp)