Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一个相当于熊猫阅读剪贴板的NumPy?_Python_Numpy_Clipboard - Fatal编程技术网

Python 一个相当于熊猫阅读剪贴板的NumPy?

Python 一个相当于熊猫阅读剪贴板的NumPy?,python,numpy,clipboard,Python,Numpy,Clipboard,例如,如果您遇到的问题/答案发布了如下数组: [[ 0 1 2 3 4 5 6 7] [ 8 9 10 11 12 13 14 15] [16 17 18 19 20 21 22 23] [24 25 26 27 28 29 30 31] [32 33 34 35 36 37 38 39] [40 41 42 43 44 45 46 47] [48 49 50 51 52 53 54 55] [56 57 58 59 60 61 62 63]] from pand

例如,如果您遇到的问题/答案发布了如下数组:

[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]
 [16 17 18 19 20 21 22 23]
 [24 25 26 27 28 29 30 31]
 [32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47]
 [48 49 50 51 52 53 54 55]
 [56 57 58 59 60 61 62 63]]
from pandas.io.clipboard import clipboard_get
# import pyperclip
import numpy as np
import re
import ast

def numpy_from_clipboard():
    inp = clipboard_get()
    # inp = pyperclip.paste()
    inp = inp.strip()
    # if it starts with "array(" we just need to remove the
    # leading "array(" and remove the optional ", dtype=xxx)"
    if inp.startswith('array('):
        inp = re.sub(r'^array\(', '', inp)
        dtype = re.search(r', dtype=(\w+)\)$', inp)
        if dtype:
            return np.array(ast.literal_eval(inp[:dtype.start()]), dtype=dtype.group(1))
        else:
            return np.array(ast.literal_eval(inp[:-1]))
    else:
        # In case it's the string representation it's a bit harder.
        # We need to remove all spaces between closing and opening brackets
        inp = re.sub(r'\]\s+\[', '],[', inp)
        # We need to remove all whitespaces following an opening bracket
        inp = re.sub(r'\[\s+', '[', inp)
        # and all leading whitespaces before closing brackets
        inp = re.sub(r'\s+\]', ']', inp)
        # replace all remaining whitespaces with ","
        inp = re.sub(r'\s+', ',', inp)
        return np.array(ast.literal_eval(inp))

您如何在REPL会话中将其加载到变量中,而不必到处添加逗号?

对于一次性情况,我可能会这样做:

  • 将包含数组的文本复制到剪贴板
  • 在ipython shell中,输入
    s=“”
    ,但不要点击回车键
  • 从剪贴板粘贴文本
  • 键入结束的三引号
这给了我:

In [16]: s = """[[ 0  1  2  3  4  5  6  7]
    ...:  [ 8  9 10 11 12 13 14 15]
    ...:  [16 17 18 19 20 21 22 23]
    ...:  [24 25 26 27 28 29 30 31]
    ...:  [32 33 34 35 36 37 38 39]
    ...:  [40 41 42 43 44 45 46 47]
    ...:  [48 49 50 51 52 53 54 55]
    ...:  [56 57 58 59 60 61 62 63]]"""
然后使用
np.loadtxt()
,如下所示:

In [17]: a = np.loadtxt([line.lstrip(' [').rstrip(']') for line in s.splitlines()], dtype=int)

In [18]: a
Out[18]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])

如果你有熊猫,pyperclip或者你可以用这样的东西:

[[ 0  1  2  3  4  5  6  7]
 [ 8  9 10 11 12 13 14 15]
 [16 17 18 19 20 21 22 23]
 [24 25 26 27 28 29 30 31]
 [32 33 34 35 36 37 38 39]
 [40 41 42 43 44 45 46 47]
 [48 49 50 51 52 53 54 55]
 [56 57 58 59 60 61 62 63]]
from pandas.io.clipboard import clipboard_get
# import pyperclip
import numpy as np
import re
import ast

def numpy_from_clipboard():
    inp = clipboard_get()
    # inp = pyperclip.paste()
    inp = inp.strip()
    # if it starts with "array(" we just need to remove the
    # leading "array(" and remove the optional ", dtype=xxx)"
    if inp.startswith('array('):
        inp = re.sub(r'^array\(', '', inp)
        dtype = re.search(r', dtype=(\w+)\)$', inp)
        if dtype:
            return np.array(ast.literal_eval(inp[:dtype.start()]), dtype=dtype.group(1))
        else:
            return np.array(ast.literal_eval(inp[:-1]))
    else:
        # In case it's the string representation it's a bit harder.
        # We need to remove all spaces between closing and opening brackets
        inp = re.sub(r'\]\s+\[', '],[', inp)
        # We need to remove all whitespaces following an opening bracket
        inp = re.sub(r'\[\s+', '[', inp)
        # and all leading whitespaces before closing brackets
        inp = re.sub(r'\s+\]', ']', inp)
        # replace all remaining whitespaces with ","
        inp = re.sub(r'\s+', ',', inp)
        return np.array(ast.literal_eval(inp))
然后阅读您保存在剪贴板中的内容:

>>> numpy_from_clipboard()
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29, 30, 31],
       [32, 33, 34, 35, 36, 37, 38, 39],
       [40, 41, 42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53, 54, 55],
       [56, 57, 58, 59, 60, 61, 62, 63]])
这应该能够从剪贴板解析(大多数)数组(
str
以及数组的
repr
)。它甚至可以用于多行数组(其中
np.loadtxt
失败):

但是我不太擅长正则表达式,所以这可能不是万无一失的,使用
ast.literal\u eval
感觉有点笨拙(但它避免了自己进行解析)


请随意提出改进建议。

我认为没有直接对应的。@juanpa.arrivillaga好吧,应该有。超方便方法ftw.Ehhh我更认为人们应该负责提供可复制的示例。打印(repr(arr))有多困难?但这是一个很好的问题。@juanpa.arrivillaga 100%同意你的观点。遗憾的是,有可复制例子的问题是濒危物种,我尽可能地珍视它们。,