I want to pass dataframe data to my clipboard so I can paste into Excel. Problem is, the character '\xe9'
is causing an encoding issue, like so:
>>> df.to_clipboard()
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pandas\util\clipboard.py", line 65, in winSetClipboard
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
TypeError: string argument without an encoding
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#51>", line 1, in <module>
df.to_clipboard()
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1028, in to_clipboard
clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
File "C:\Python34\lib\site-packages\pandas\io\clipboard.py", line 98, in to_clipboard
clipboard_set(objstr)
File "C:\Python34\lib\site-packages\pandas\util\clipboard.py", line 68, in winSetClipboard
hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 543793: ordinal not in range(128)
I decoded the character and it's an accent é
>>> '\xe9'.encode().decode()
'é'
After reading the documentation for to_clipboard(), I noticed it says:
other keywords are passed to to_csv
. OK, so by 'other keywords', I assume that means keyword arguments from to_csv() -- specifically I want to use encoding='cp1252'
.
When I try this, to_clipboard()
doesn't recognize the encoding
keyword:
df.to_clipboard(encoding='cp1252')
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 1028, in to_clipboard
clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
File "C:\Python34\lib\site-packages\pandas\io\clipboard.py", line 95, in to_clipboard
objstr = obj.to_string(**kwargs)
TypeError: to_string() got an unexpected keyword argument 'encoding'
Is there a way to pass all data to the clipboard (both ascii and non-ascii)?
df.to_clipboard(df.to_csv(encoding='cp1252'))
just encode it as a csv with the encoding specified then throw it on the clipboard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With