Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pandas/io/clipboards.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1""" io on the clipboard """
2from io import StringIO
3import warnings
5from pandas.core.dtypes.generic import ABCDataFrame
7from pandas import get_option, option_context
10def read_clipboard(sep=r"\s+", **kwargs): # pragma: no cover
11 r"""
12 Read text from clipboard and pass to read_csv.
14 Parameters
15 ----------
16 sep : str, default '\s+'
17 A string or regex delimiter. The default of '\s+' denotes
18 one or more whitespace characters.
20 **kwargs
21 See read_csv for the full argument list.
23 Returns
24 -------
25 DataFrame
26 A parsed DataFrame object.
27 """
28 encoding = kwargs.pop("encoding", "utf-8")
30 # only utf-8 is valid for passed value because that's what clipboard
31 # supports
32 if encoding is not None and encoding.lower().replace("-", "") != "utf8":
33 raise NotImplementedError("reading from clipboard only supports utf-8 encoding")
35 from pandas.io.clipboard import clipboard_get
36 from pandas.io.parsers import read_csv
38 text = clipboard_get()
40 # Try to decode (if needed, as "text" might already be a string here).
41 try:
42 text = text.decode(kwargs.get("encoding") or get_option("display.encoding"))
43 except AttributeError:
44 pass
46 # Excel copies into clipboard with \t separation
47 # inspect no more then the 10 first lines, if they
48 # all contain an equal number (>0) of tabs, infer
49 # that this came from excel and set 'sep' accordingly
50 lines = text[:10000].split("\n")[:-1][:10]
52 # Need to remove leading white space, since read_csv
53 # accepts:
54 # a b
55 # 0 1 2
56 # 1 3 4
58 counts = {x.lstrip().count("\t") for x in lines}
59 if len(lines) > 1 and len(counts) == 1 and counts.pop() != 0:
60 sep = "\t"
62 # Edge case where sep is specified to be None, return to default
63 if sep is None and kwargs.get("delim_whitespace") is None:
64 sep = r"\s+"
66 # Regex separator currently only works with python engine.
67 # Default to python if separator is multi-character (regex)
68 if len(sep) > 1 and kwargs.get("engine") is None:
69 kwargs["engine"] = "python"
70 elif len(sep) > 1 and kwargs.get("engine") == "c":
71 warnings.warn(
72 "read_clipboard with regex separator does not work "
73 "properly with c engine"
74 )
76 return read_csv(StringIO(text), sep=sep, **kwargs)
79def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
80 """
81 Attempt to write text representation of object to the system clipboard
82 The clipboard can be then pasted into Excel for example.
84 Parameters
85 ----------
86 obj : the object to write to the clipboard
87 excel : boolean, defaults to True
88 if True, use the provided separator, writing in a csv
89 format for allowing easy pasting into excel.
90 if False, write a string representation of the object
91 to the clipboard
92 sep : optional, defaults to tab
93 other keywords are passed to to_csv
95 Notes
96 -----
97 Requirements for your platform
98 - Linux: xclip, or xsel (with PyQt4 modules)
99 - Windows:
100 - OS X:
101 """
102 encoding = kwargs.pop("encoding", "utf-8")
104 # testing if an invalid encoding is passed to clipboard
105 if encoding is not None and encoding.lower().replace("-", "") != "utf8":
106 raise ValueError("clipboard only supports utf-8 encoding")
108 from pandas.io.clipboard import clipboard_set
110 if excel is None:
111 excel = True
113 if excel:
114 try:
115 if sep is None:
116 sep = "\t"
117 buf = StringIO()
119 # clipboard_set (pyperclip) expects unicode
120 obj.to_csv(buf, sep=sep, encoding="utf-8", **kwargs)
121 text = buf.getvalue()
123 clipboard_set(text)
124 return
125 except TypeError:
126 warnings.warn(
127 "to_clipboard in excel mode requires a single character separator."
128 )
129 elif sep is not None:
130 warnings.warn("to_clipboard with excel=False ignores the sep argument")
132 if isinstance(obj, ABCDataFrame):
133 # str(df) has various unhelpful defaults, like truncation
134 with option_context("display.max_colwidth", None):
135 objstr = obj.to_string(**kwargs)
136 else:
137 objstr = str(obj)
138 clipboard_set(objstr)