2016-10-02 30 views
0

基本上我从https://en.wikipedia.org/wiki/List_of_lists_of_lists复制了一大堆列表到我的剪贴板中。 当我运行我的程序时,它会在每行之后添加项目符号。(Python)帮助修改剪贴板中的字符串

例如:

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•• 
+0

你的问题是什么?什么是“文字”的类型?它是'str'元素的'list'吗? – blacksite

+0

对不起,这个类型是一个复制到剪贴板的字符串。 – tadm123

回答

1

做一个小的变化,以您的代码(使用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) 

通常,“新行”是指任何的字符集通常被解释为信令新行,其可包括:在上DOS/Windows的

  • CR

    • CR LF在Unix上老的Mac
    • LF变种,包括现代的Mac

    CR是回车ASCII字符(代码0X0D),通常表示为\ r。 LF是换行符(代码0x0A),通常表示为\ n。

    而且,这样说的:https://blog.codinghorror.com/the-great-newline-schism/

    我只是想让你写一个平台无关的解决方案。因此os.linesep

  • +0

    非常感谢..这是工作。你能告诉我什么是我做错了吗?非常奇怪的是它不适用于'\ n'字符。 – tadm123

    +0

    我明白了......再次感谢。 – tadm123