首页 > 解决方案 > 使用 Python3 删除新行

问题描述

如何使用 python 删除垂直制表符和退格(字符 ASCII 代码 013 010)?我尝试以下代码但不工作。

text = re.sub('\n', ' ', text) // after execute, removed all text except for last line
text = text.replace('\n',' ') // after execute, removed all text except for last line
text = text.rstrip() 
text = text.lstrip()
text = text.strip()

这是一个原始字符串:

Only certified persons are allowed to use the research equipment and facilities
 at NSL. There are several things users need to do before they are given access
 to the labs. All new users must complete steps 1-3 (and step 4 for those planning 
 to perform chemical operations at NSL.)


 Every user must complete the user form and submit it to nanosystemslaboratory@osu.edu. 
 Internal users (OSU users) must fill the erequest number under the chartfield section of the user form. 
 Failure to provide an erequest number for the associated chartfield may delay processing of the user form.

所需的输出是单行文本,例如:line1 line2 line3

Only certified persons ... Every user must complete the user ...

标签: pythonpython-3.x

解决方案


我想你想尝试的是line.strip()。它适用于空格和制表符,并保留其间的空格。

line = '   this is my example          '
line.strip() == 'this is my example'

推荐阅读