移除神秘的断线Python

2024-10-05 13:20:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在文本文件中有这样的代码:

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
".

我想删除这个换行符,因为它不允许文本被格式化


Tags: orghttpexamplewwwtypessoffsetperson
1条回答
网友
1楼 · 发布于 2024-10-05 13:20:25

.split()不带参数或作为第一个参数的None首先删除行周围的空格,但.split('|')。你知道吗

在拆分之前明确删除它:

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

或之后:

gloss = L2[1].strip()

.strip()删除所有的前导和尾随空格。您可以更具体地使用`.rstrip(),只从末尾删除换行符:

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

相关问题 更多 >

    热门问题