2013-06-13 50 views
0

我有這樣的代碼,通過在文本文件中的行像這樣運行:去除神祕換行符蟒蛇

09824747 18 n 02 archer 0 bowman 0 003 @ 09640897 n 0000 ~ 10290474 n 0000 ~i 10718145 n 0000 | a person who is expert in the use of a bow and arrow

L = line.split() 
L2 = line.split('|') 
synset_offset = L[0] 
lex_filenum = L[1] 
ss_type = L[2] 
gloss = L2[1] 

他們這樣,我打印這些出看起來像這樣

print('''<http://example.org/#'''+synset_offset+'''><http://www.monnetproject.eu/lemon#lex_filenum> "'''+lex_filenum+'''". 
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#ss_type> "'''+ss_type+'''". 
<http://example.org/#'''+synset_offset+'''> <http://www.monnetproject.eu/lemon#gloss> "'''+gloss+'''".''') 

但由於某種原因發生換行後'''+gloss+'''

,看起來像這樣

<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#lex_filenum> "18". 
<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#ss_type> "n". 
<http://example.org/#09824747> <http://www.monnetproject.eu/lemon#gloss> " a person who is expert in the use of a bow and arrow 
". 

我想刪除斷行,因爲它不會允許文本進行,否則格式化

+0

而且 - 其他scritp HTML生成語言如PHP和JavaScript缺乏一個內置的字符串模板系統,並要求該組合您正在使用的引號和+符號(或。)。 Python的情況並非如此 - 如果您使用以下任一類型的字符串格式,您的HTML片段可以變得更具可讀性兩個數量級:http://docs.python.org/2/library/stdtypes.html#string-formatting – jsbueno

回答

4

.split()不帶參數或None作爲第一個參數首先除去周圍的線的空白,但.split('|')不是

分裂之前明確地將其刪除:

L2 = line.strip().split('|') 

以後:

gloss = L2[1].strip() 

.strip()刪除所有前後空白。您可以更具體,只刪除使用`.rstrip()從最終的換行符:

gloss = L2[1].rstrip('\n')