Skip to content

Add function to read DataFrame from clipboard across platforms #300

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

Closed
wesm opened this issue Oct 27, 2011 · 6 comments
Closed

Add function to read DataFrame from clipboard across platforms #300

wesm opened this issue Oct 27, 2011 · 6 comments
Milestone

Comments

@wesm
Copy link
Member

wesm commented Oct 27, 2011

No description provided.

@takluyver
Copy link
Contributor

There's code in IPython for cross-platform clipboard access - it's intended for text, but it could probably be adapted:

https://github.com/ipython/ipython/blob/master/IPython/core/hooks.py
https://github.com/ipython/ipython/blob/master/IPython/lib/clipboard.py

@wesm
Copy link
Member Author

wesm commented Nov 14, 2011

Thanks Thomas. I just copied that code into pandas.util.clipboard for now and added a new function read_clipboard which works nicely. Not sure how to test it but I guess I'll just make sure "it works" on all the platforms :)

@wesm wesm closed this as completed Nov 14, 2011
@takluyver
Copy link
Contributor

Great, thanks. It seems to be working on Linux, copying from LO Calc, in Python 2 and 3. Although it does choke on a non-ascii character in Python 2.

@wesm
Copy link
Member Author

wesm commented Nov 14, 2011

Ah, hm. If you figure out a patch to make it work let me know

@takluyver
Copy link
Contributor

I think the trouble is that Python 2's CSV module doesn't handle unicode properly. There are some examples of wrappers in the csv library docs, if it's a serious concern.

It's also likely to be dependent on platform. The Linux clipboard code returns unicode, the Mac code looks like it will return a byte string, and I've no idea about the Windows code. For that matter, I suspect that the Mac code will fail on Python 3 - StringIO.StringIO gets translated to io.StringIO, which only allows unicode. io.BytesIO is the bytes counterpart, but I think the csv module in Python 3 expects unicode.

@fccoelho
Copy link

I am having some issues with row labels containing unicode characters. I am using pandas 0.8.1, python 2.7.3. Here is the error message I am getting:


UnicodeEncodeError Traceback (most recent call last)
in ()
----> 1 pd.read_clipboard(sep='\t')

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in read_clipboard(*_kwargs)
351 from pandas.util.clipboard import clipboard_get
352 text = clipboard_get()
--> 353 return read_table(StringIO(text), *_kwargs)
354
355 def to_clipboard(obj): # pragma: no cover

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in read_table(filepath_or_buffer, sep, dialect, header, index_col, names, skiprows, na_values, thousands, comment, parse_dates, keep_date_col, dayfirst, date_parser, nrows, iterator, chunksize, skip_footer, converters, verbose, delimiter, encoding, squeeze)
282 kwds['encoding'] = None
283
--> 284 return _read(TextParser, filepath_or_buffer, kwds)
285
286 @appender(_read_fwf_doc)

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in _read(cls, filepath_or_buffer, kwds)
182
183 # Create the parser.
--> 184 parser = cls(f, **kwds)
185
186 if nrows is not None:

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in init(self, f, delimiter, dialect, names, header, index_col, na_values, thousands, comment, parse_dates, keep_date_col, date_parser, dayfirst, chunksize, skiprows, skip_footer, converters, verbose, encoding, squeeze)
494 self._name_processed = False
495 if not self._has_complex_date_col:
--> 496 self.index_name = self._get_index_name()
497 self._name_processed = True
498 self._first_chunk = True

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in _get_index_name(self, columns)
671
672 try:
--> 673 next_line = self._next_line()
674 except StopIteration:
675 next_line = None

/usr/local/lib/python2.7/dist-packages/pandas/io/parsers.pyc in _next_line(self)
604 self.pos += 1
605
--> 606 line = next(self.data)
607
608 line = self._check_comments([line])[0]

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 9: ordinal not in range(128)

dan-nadler pushed a commit to dan-nadler/pandas that referenced this issue Sep 23, 2019
Use `.to_pydatetime` instead which has been in pandas since 0.10.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants