我在文本文件中有这样的代码:
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
".
我想删除这个换行符,因为它不允许文本被格式化
.split()
不带参数或作为第一个参数的None
首先删除行周围的空格,但.split('|')
不而。你知道吗在拆分之前明确删除它:
或之后:
.strip()
删除所有的前导和尾随空格。您可以更具体地使用`.rstrip(),只从末尾删除换行符:相关问题 更多 >
编程相关推荐