- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将多行添加到文件(input.txt)并替换同一文件(input.txt)中的多行,但我的代码仅在文件末尾插入行。你知道我如何修复代码以获得我想要的预期输出文件吗?
我的代码:
import re
searchtext1 = """
AB 3.483e-01 2.52e-02 ; 3.46 0.0123
"""
# add these lines after searchtext1
addtext1 = """
CD 2.123e-01 1.31e-02 ; 7.25 0.0145
DE 4.896e-01 7.25e-02 ; 8.25 0.0185
"""
searchtext2 = """
; atom
#atomnumber
#molecule
[weight]
"""
# replace these lines to searchtext2
changetext2 = """
; iron
#48kcal
35 mol
#12 g
"""
with open('input.txt', 'ab+') as infile:
matches1 = re.findall(r'^(\d+)\.(.*)$', searchtext1, re.MULTILINE)
infile.write(addtext1)
matches2 = re.findall(r'^(\d+)\.(.*)$', searchtext2, re.MULTILINE)
infile.write(changetext2)
输入.txt:
[atom]
123
[bonds]
XY 4.212e-01 4.18e-02 ; 8.01 0.0487
AB 3.483e-01 2.52e-02 ; 3.46 0.0123
[molecule]
1 2
3 4
TY 0.412e-01 1.72e-02 ; 0.32 0.0455
; atom
#atomnumber
#molecule
[weight]
calculated value is 5 kcal/mol
end file
预期输出文件:
[atom]
123
[bonds]
XY 4.212e-01 4.18e-02 ; 8.01 0.0487
AB 3.483e-01 2.52e-02 ; 3.46 0.0123
CD 2.123e-01 1.31e-02 ; 7.25 0.0145
DE 4.896e-01 7.25e-02 ; 8.25 0.0185
[molecule]
1 2
3 4
TY 0.412e-01 1.72e-02 ; 0.32 0.0455
; iron
#48kcal
35 mol
#12 g
calculated value is 5 kcal/mol
end file
最佳答案
正如 Chris 在评论中提到的,我建议尝试使用一个已经可以与格式交互的库 - configparser
对我来说似乎是显而易见的选择,但如果格式可能与您问题中的内容不同,则可能有理由不这样做。
除此之外,如果您想使用 re
,我稍微更新了你的代码。这是我所做的调整:
从三引号字符串的开头和结尾删除了换行符,因为它们将成为匹配的一部分,这可能是也可能不是您想要的。我个人的偏好是在实际替换中明确换行符。
转义 [
和]
在您的搜索文本中,因为这些是 re
中的特殊字符并会混淆它(它们指定一个字符类)。
二手with
打开单独的输入和输出文件。当您退出 with
时,这些会被清除。堵塞。如果您确实想替换 input.txt,我想您可以稍后将 output.txt 移到顶部。
您可以使用re.sub
直接替换出现的内容,而不是搜索它们,在文本中查找索引,然后替换/追加。在添加文本的情况下,我刚刚将 searchtext1
进行了替换和addtext1
。 (我使用了 f 弦,但如果您愿意,也可以这样做 "{search}\n{add}".format(search=searchtext, add=addtext1)
。
最后我们将更新后的文本写回光盘。
希望有帮助!
import re
searchtext1 = """AB 3.483e-01 2.52e-02 ; 3.46 0.0123"""
# add these lines after searchtext1
addtext1 = """CD 2.123e-01 1.31e-02 ; 7.25 0.0145
DE 4.896e-01 7.25e-02 ; 8.25 0.0185"""
searchtext2 = """; atom
#atomnumber
#molecule
\[weight\]"""
# replace these lines to searchtext2
changetext2 = """; iron
#48kcal
35 mol
#12 g"""
with open('input.txt', 'r') as infile, open("output.txt", "w") as outfile:
intext = infile.read()
intext = re.sub(searchtext1, f"{searchtext1}\n{addtext1}", intext)
intext = re.sub(searchtext2, changetext2, intext)
outfile.write(intext)
关于python - 使用 findall 替换并添加多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126527/
如何通过特定条件获取值,然后使用这些选定元素从其他事实系列中获取值? 我有这个代码 %code, date, amount values1('AAA', date(02, 03, 2020), 100
我是 Python 的新手,主要需要它来从网站获取信息。在这里,我试图从网站底部获取简短的标题,但无法完全获取。 from bfs4 import BeautifulSoup import reque
我不断收到错误“缺少 1 个必需的位置参数:'section_url'” 每次我尝试使用 findall 时都会收到此错误。 刚开始学习 python,因此我们将不胜感激! from bs4 impo
我有这张 table 。客户有项目,用户在项目中工作 Clients - id - name Projects - id - name - client_id Users - id - name Us
嗨,我是 Python 和 Beautiful 汤的新手。我试图仅从表格的某个部分获取文本。但似乎 findAll 的结果不是我可以再次运行 findAll 的 BeautifulSoup 类型。 s
登录 @ApiModel @Entity public class Login { @Id @GeneratedValue(strategy = GenerationType.AUTO
登录 @ApiModel @Entity public class Login { @Id @GeneratedValue(strategy = GenerationType.AUTO
有什么方法可以防止 Spring Data REST 为覆盖的存储库方法创建/search URL? 例如,以下代码会生成一个/search/findAll URL,它复制了集合资源的功能: publ
有什么方法可以防止 Spring Data REST 为覆盖的存储库方法创建/search URL? 例如,以下代码会生成一个/search/findAll URL,它复制了集合资源的功能: publ
使用 Spring Data JpaRepository 可以通过某种排序获取给定 Id 的选择集合。这意味着我需要启用以下查询。我找到了一些 solution应用@NamedQuery 但我无法启用
我正在尝试在我的 Express 应用程序中使用 Sequelize 获取数据,并使用 MSSQL 获取数据库。这是我的代码: getInstitution: function (req, res)
代码应该在请求/tasks 时返回一个带有空任务的 JSON 对象,而是返回一条消息错误 - TypeError: cannot read property 'findAll' of undefine
我的python版本是2.7.6 我知道 +? 是 + 的非贪婪版本。 这样 re.findall('(ab)+?', 'abab') 将匹配尽可能少的 ab。 结果 ['ab', 'ab'] 因此有
我正在使用 sequelize 从 mySql db 获取数据。这就是我如何使用它 const isProduct = await models.product.findAll({ where:
ItemTag对象包含一个Item对象和一个Tag对象。 (这些是Java域对象。) 这个简单的查询按预期工作。我返回一个ItemTags列表,并且可以完成ItemTags应该做的所有奇妙的事情: d
试图简单地使用find all运行域对象的查询,并且它的行为不像我期望的那样: searchResults = Contact.findAll("from Contact as c where c.c
我想使用 groovy findAll 和我的参数来过滤闭包 filterClosure = { it, param -> it.getParam == param } 我现在如何在 findAl
我扩展了 CrudRepository 来创建一个名为 TaskDao 的类。我认为 taskDao.findall() 会从数据库中提取值。由于某种原因,taskDao.findall() 实际上返
我正在开发一个 grails 应用程序,在此我必须在 list.gsp 上应用过滤器框。当我使用以下查询(在我的服务中)进行过滤时,我得到了分页列表: def clientCriteria =
我正在尝试创建一个正则表达式来查找 Perl 代码中的所有变量。 变量如下所示:$variable_test。 所以这是我使用的正则表达式: ^\$\w+$ 这给了我Python中的这一行: matc
我是一名优秀的程序员,十分优秀!