python - 使用 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 ...
解决方案
我想你想尝试的是line.strip()
。它适用于空格和制表符,并保留其间的空格。
line = ' this is my example '
line.strip() == 'this is my example'
推荐阅读
- javascript - 更改用于分隔具有相同名称标识符的表单输入字段的字符?
- django - 动态更新 form_list 以指向某个步骤的某个子类(仅在运行时已知),并呈现该表单
- python - 通过 Numpy/Pandas 使用 (nx 1) 数据创建多项式的 anxm 数组
- sql - 如何两次输出同一列,受具有 COUNT 函数的不同 WHERE 子句的影响
- modelica - OpenModelica 中 StopTime 过高时 FFT 的仿真结果错误或没有
- django - Django 新创建的实例 - DateTime 字段类型为“str”
- php - 具有两个字段的数组上的 Laravel 验证存在于数据库行上
- symfony4 - 选中一个按钮单选时,显示 dateTypefrom,如果不是从 TextType 获取值
- android - 如何在片段中按钮的可见性上使用 onSaveInstanceState?
- python - 将关键字插入 .inp 文件的 Abaqus Python 命令