2016-10-02 3 views
0

基本的にはhttps://en.wikipedia.org/wiki/List_of_lists_of_listsのリストをクリップボードにコピーしています。 プログラムを実行すると、各行の前後に行頭文字が追加されます。例えば(Python)クリップボードから文字列を変更する際のヘルプ

:等々

•• Lists of Iranian films •• 

そして:

Lists of Iranian films 

がに変換します。このプログラムは、行の前に箇条書きを追加するときに機能しますが、後ろに置くと改行文字なしの長い文字列が1つだけ表示されます。誰かが私に間違っていることを教えてもらえますか?

ここでは、コードです:私のクリップボードに

#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

あなたの質問がありますか? 'text'の種類は何ですか? '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 LF
  • CRオンUnixで古いMac
  • LFは、通常は\ rと表さキャリッジリターンASCII文字(コード0x0Dが)、現代のMac

CRさを含む、変異体です。 LFはラインフィード文字(コード0x0A)で、通常\ nで表されます。私はちょうどあなたが、プラットフォームに依存しないソリューションを書きたいhttps://blog.codinghorror.com/the-great-newline-schism/

また、これをお読みください。したがってos.linesep

+0

ありがとう..これは動作します。私は何が間違っていたのか教えていただけますか?それは '\ n'文字ではうまくいかないというのは非常に奇妙です。 – tadm123

+0

私は参照してください..もう一度ありがとう。 – tadm123

関連する問題