Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/pandas/io/clipboards.py
7815 views
""" io on the clipboard """1from __future__ import annotations23from io import StringIO4import warnings56from pandas.core.dtypes.generic import ABCDataFrame78from pandas import (9get_option,10option_context,11)121314def read_clipboard(sep: str = r"\s+", **kwargs): # pragma: no cover15r"""16Read text from clipboard and pass to read_csv.1718Parameters19----------20sep : str, default '\s+'21A string or regex delimiter. The default of '\s+' denotes22one or more whitespace characters.2324**kwargs25See read_csv for the full argument list.2627Returns28-------29DataFrame30A parsed DataFrame object.31"""32encoding = kwargs.pop("encoding", "utf-8")3334# only utf-8 is valid for passed value because that's what clipboard35# supports36if encoding is not None and encoding.lower().replace("-", "") != "utf8":37raise NotImplementedError("reading from clipboard only supports utf-8 encoding")3839from pandas.io.clipboard import clipboard_get40from pandas.io.parsers import read_csv4142text = clipboard_get()4344# Try to decode (if needed, as "text" might already be a string here).45try:46text = text.decode(kwargs.get("encoding") or get_option("display.encoding"))47except AttributeError:48pass4950# Excel copies into clipboard with \t separation51# inspect no more then the 10 first lines, if they52# all contain an equal number (>0) of tabs, infer53# that this came from excel and set 'sep' accordingly54lines = text[:10000].split("\n")[:-1][:10]5556# Need to remove leading white space, since read_csv57# accepts:58# a b59# 0 1 260# 1 3 46162counts = {x.lstrip(" ").count("\t") for x in lines}63if len(lines) > 1 and len(counts) == 1 and counts.pop() != 0:64sep = "\t"65# check the number of leading tabs in the first line66# to account for index columns67index_length = len(lines[0]) - len(lines[0].lstrip(" \t"))68if index_length != 0:69kwargs.setdefault("index_col", list(range(index_length)))7071# Edge case where sep is specified to be None, return to default72if sep is None and kwargs.get("delim_whitespace") is None:73sep = r"\s+"7475# Regex separator currently only works with python engine.76# Default to python if separator is multi-character (regex)77if len(sep) > 1 and kwargs.get("engine") is None:78kwargs["engine"] = "python"79elif len(sep) > 1 and kwargs.get("engine") == "c":80warnings.warn(81"read_clipboard with regex separator does not work properly with c engine."82)8384return read_csv(StringIO(text), sep=sep, **kwargs)858687def to_clipboard(88obj, excel: bool | None = True, sep: str | None = None, **kwargs89) -> None: # pragma: no cover90"""91Attempt to write text representation of object to the system clipboard92The clipboard can be then pasted into Excel for example.9394Parameters95----------96obj : the object to write to the clipboard97excel : bool, defaults to True98if True, use the provided separator, writing in a csv99format for allowing easy pasting into excel.100if False, write a string representation of the object101to the clipboard102sep : optional, defaults to tab103other keywords are passed to to_csv104105Notes106-----107Requirements for your platform108- Linux: xclip, or xsel (with PyQt4 modules)109- Windows:110- OS X:111"""112encoding = kwargs.pop("encoding", "utf-8")113114# testing if an invalid encoding is passed to clipboard115if encoding is not None and encoding.lower().replace("-", "") != "utf8":116raise ValueError("clipboard only supports utf-8 encoding")117118from pandas.io.clipboard import clipboard_set119120if excel is None:121excel = True122123if excel:124try:125if sep is None:126sep = "\t"127buf = StringIO()128129# clipboard_set (pyperclip) expects unicode130obj.to_csv(buf, sep=sep, encoding="utf-8", **kwargs)131text = buf.getvalue()132133clipboard_set(text)134return135except TypeError:136warnings.warn(137"to_clipboard in excel mode requires a single character separator."138)139elif sep is not None:140warnings.warn("to_clipboard with excel=False ignores the sep argument.")141142if isinstance(obj, ABCDataFrame):143# str(df) has various unhelpful defaults, like truncation144with option_context("display.max_colwidth", None):145objstr = obj.to_string(**kwargs)146else:147objstr = str(obj)148clipboard_set(objstr)149150151