Pyperclip复制并划分结果



我的代码没有划分复制的答案,而是给我字符串错误。

for x in range(5):
time.sleep(3)
coordinates = (pyperclip.paste())
print(coordinates/2) 

TypeError: unsupported operand type(s) for /: 'objc.pyobjc_unicode' and 'int'

如果你使用了pyperclip.copy("1234")并且字符串是一个数字,那么你可以这样做

for x in range(5):
time.sleep(3)
coordinates = int(pyperclip.paste())
print(coordinates/2)

当你看到官方网站的主页:https://pypi.org/project/pyperclip/

则有以下代码:

>>> import pyperclip
>>> pyperclip.copy('The text to be copied to the clipboard.')
>>> pyperclip.paste()
'The text to be copied to the clipboard.'

函数本质上不给出坐标,但你会得到被复制的东西。

如果要复制文本,则不能将文本除以整数。如果您复制一个数字,请记住这个数字是一个字符串,直到您将其定义为整数。为此,使用int()函数。比如说你复制了一个'2':

import pyperclip
pyperclip.copy('2')
print(int(pyperclip.paste())/2)

无论如何,在你的情况下,它不会像你试图获得坐标一样工作。坐标= (x, y)会生成异常

c=(1,2)
c/2
Retraçage (dernier appel le plus récent) : 
Shell Python, prompt 23, line 1
builtins.TypeError: unsupported operand type(s) for /: 'tuple' and 'int'

假设坐标是字符串'(1,2)',那么你可以有新的坐标计算函数:

coordinates = '(1,2)'
def new_coord(coordinates, func):
return tuple(map(func, coordinates.strip("()").split(",")))

print(new_coord(coordinates, func=lambda c: int(c)/2))
# (0.5, 1.0)

最新更新