0
我遇到的問題得到以下代碼才能正確粘貼。它的想法是從文件中讀取'n'行數,然後將這些行移動到剪貼板並將其粘貼到用戶光標位置所在的位置。從文件中一次複製x行,然後將它們粘貼到Python中的(^ v)
理想我想有一個看起來像這樣的文件:
the dog says bark
the cat says meow
程序會讀這兩條線爲一個字符串(next_n_lines),然後將它們粘貼,但我得到的輸出是「[ 「對,狗,說,樹皮\ n此,貓,說,喵」]」。如果我只是手動複製這些行,我需要顯示新行和空格。
def main():
from itertools import islice
import win32com.client, time, pyperclip
shell = win32com.client.Dispatch("WScript.Shell")
userfile = raw_input("Please enter the file you wish to open\n(must be in this directory): ")
file1 = open(userfile, "r+")
n = 18
time.sleep(3)
while True:
next_n_lines = list(islice(file1,n))
print ''.join(next_n_lines)
if not next_n_lines:
break
pyperclip.copy(next_n_lines)
shell.sendkeys("^v")
break
pass
file1.close()
~~~~~~~~~~~~~~~~~~~我自己修復了。
def main():
from itertools import islice
import win32com.client, time, pyperclip
shell = win32com.client.Dispatch("WScript.Shell")
userfile = raw_input("Please enter the file you wish to open\n(must be in this directory): ")
file1 = open(userfile, "r+")
n = 18
time.sleep(3)
while True:
next_n_lines = list(islice(file1,n))
print ''.join(next_n_lines)
if not next_n_lines:
break
pyperclip.copy(''.join(next_n_lines) #~~~~~~~~~~~~~~~(''.join needed to be added
shell.sendkeys("^v")
break
pass
file1.close()
什麼是'魔法'?爲什麼要睡覺(3)? – akaRem
magic n將是我想複製/粘貼的行數。假設我粘貼的東西只允許一次只允許50行,我將n設置爲15. 在這種情況下,n設置爲18,islice一次只需要18行。 睡眠3就是這樣你可以把鼠標放到位置 – Mason
「狗說樹皮 貓說喵」。探究的問題:狐狸說什麼? – inspectorG4dget