(Python) 帮助修改剪贴板中的字符串
(Python) Help in modfiying a string from the clipboard
基本上,我将 https://en.wikipedia.org/wiki/List_of_lists_of_lists 中的一堆列表复制到我的剪贴板中。
当我 运行 我的程序时,它会在每一行前后添加项目符号。
例如:
Lists of Iranian films
将转换为:
•• Lists of Iranian films ••
等等。当我在该行之前添加项目符号时,该程序有效,但当我将它们放在它之后时,它只打印一个没有任何换行符的长字符串。谁能告诉我我做错了什么?
代码如下:
#bulletPointAdder.py - Adds Wikipedia bullet points to the start and end
#of each line of text on the clipboard
import pyperclip
text=pyperclip.paste() #paste a big string of text from clipboard into the 'text' string
# Separate lines and add stars
lines = text.split('\n') #'lines' contains a list of all the individual lines up until '\n'
#lines= ['list of iserael films', 'list of italian films' ...]
for i in range(len(lines)): #loop through all indexes in the "lines" list
lines[i] = '••' + lines[i] + '••' #add bullets before and after each line
text = '\n'.join(lines) #put a '\n' in between the list members (joins them) into a single string
pyperclip.copy(text)
在我的剪贴板中:
List of Israeli films before 1960
List of Israeli films of the 1960s
List of Israeli films of the 1970s
List of Israeli films of the 1980s
在记事本中粘贴的剪贴板:
••List of Israeli films before 1960••••List of Israeli films of the 1960s••••List of Israeli films of the 1970s••••List of Israeli films of the 1980s••
对您的代码做一点小改动(使用 os.linesep 而不是 '\n'
):
import os
import pyperclip
text=pyperclip.paste()
#paste will paste a big string of text in 'text' string
# Separate lines and add stars
lines = text.split(os.linesep) #lines contains a list of all the individual lines up cut before newline
#lines= ['list of iserael films', 'list of italian films' ...]
for i in range(len(lines)): #loop through all indexes in the "lines" list
lines[i] = '••' + lines[i] + '••' #add bullets before and after each line
text = os.linesep.join(lines) #put a newline in between the list members (joins them) into a single string
pyperclip.copy(text)
通常,"new line" 指的是通常被解释为表示换行的任何字符集,其中包括:
- CR LF DOS/Windows
- 旧款 Mac 上的 CR
- Unix 变体(包括现代 Mac)上的 LF
CR是Carriage Return ASCII字符(Code 0x0D),通常表示为\r。 LF 是换行字符(代码 0x0A),通常表示为 \n.
另请阅读:https://blog.codinghorror.com/the-great-newline-schism/
我只是想让你写一个与平台无关的解决方案。因此 os.linesep
基本上,我将 https://en.wikipedia.org/wiki/List_of_lists_of_lists 中的一堆列表复制到我的剪贴板中。 当我 运行 我的程序时,它会在每一行前后添加项目符号。
例如:
Lists of Iranian films
将转换为:
•• Lists of Iranian films ••
等等。当我在该行之前添加项目符号时,该程序有效,但当我将它们放在它之后时,它只打印一个没有任何换行符的长字符串。谁能告诉我我做错了什么?
代码如下:
#bulletPointAdder.py - Adds Wikipedia bullet points to the start and end
#of each line of text on the clipboard
import pyperclip
text=pyperclip.paste() #paste a big string of text from clipboard into the 'text' string
# Separate lines and add stars
lines = text.split('\n') #'lines' contains a list of all the individual lines up until '\n'
#lines= ['list of iserael films', 'list of italian films' ...]
for i in range(len(lines)): #loop through all indexes in the "lines" list
lines[i] = '••' + lines[i] + '••' #add bullets before and after each line
text = '\n'.join(lines) #put a '\n' in between the list members (joins them) into a single string
pyperclip.copy(text)
在我的剪贴板中:
List of Israeli films before 1960
List of Israeli films of the 1960s
List of Israeli films of the 1970s
List of Israeli films of the 1980s
在记事本中粘贴的剪贴板:
••List of Israeli films before 1960••••List of Israeli films of the 1960s••••List of Israeli films of the 1970s••••List of Israeli films of the 1980s••
对您的代码做一点小改动(使用 os.linesep 而不是 '\n'
):
import os
import pyperclip
text=pyperclip.paste()
#paste will paste a big string of text in 'text' string
# Separate lines and add stars
lines = text.split(os.linesep) #lines contains a list of all the individual lines up cut before newline
#lines= ['list of iserael films', 'list of italian films' ...]
for i in range(len(lines)): #loop through all indexes in the "lines" list
lines[i] = '••' + lines[i] + '••' #add bullets before and after each line
text = os.linesep.join(lines) #put a newline in between the list members (joins them) into a single string
pyperclip.copy(text)
通常,"new line" 指的是通常被解释为表示换行的任何字符集,其中包括:
- CR LF DOS/Windows
- 旧款 Mac 上的 CR
- Unix 变体(包括现代 Mac)上的 LF
CR是Carriage Return ASCII字符(Code 0x0D),通常表示为\r。 LF 是换行字符(代码 0x0A),通常表示为 \n.
另请阅读:https://blog.codinghorror.com/the-great-newline-schism/
我只是想让你写一个与平台无关的解决方案。因此 os.linesep