根据逻辑删除一行
我有一个文件,其中有多个包含此类数据的记录
F00DY4302B8JRQ 排名=0000030 x=800.0 y=1412.0 长度=89
现在我想搜索行,如果我发现length<=50,则删除该行和文件中的下一行并写入另一个文件。
谢谢大家
I have a file where i have multiple records with such data
F00DY4302B8JRQ rank=0000030 x=800.0 y=1412.0 length=89
now i want to search for the line where if i find length<=50 then delete this line and the next line in the file and write to another file.
Thanks everyone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从我的脑海中:
希望这是你开始工作所需要的。
From the top of my head:
Hope this is what you need to start working.
假设 Python 2.6(请告诉我们您是否需要另一个版本!),并且您希望跳过长度 <= 50 的每一行(并忽略每种情况下的下一行)(如果有) :
如果这不完全是您的规格,请澄清。
Assuming Python 2.6 (let us know if it's another version you need!), and that you want to skip every line with length <= 50 (and ignore the next line in each case), if any:
If that's not exactly your specs, please clarify.
如果列总是以相同的顺序并且总是有相同的数字,您可以对字符串使用
.split()
方法,并通过索引找到您想要的列:列可以按任何顺序排列,您可以使用正则表达式。
这使用正则表达式中的“匹配组”来获取数字。
PS 当我写这篇文章时出现了两个答案。我不是西方跑得最快的枪。
If the columns are always in the same order and there are always the same number, you can just use the
.split()
method on the string, and find the one you want with an index:If the columns might be in any order, you could use regular expressions.
This uses the "match group" from the regular expression to grab the number.
P.S. Two answers appeared while I was writing this. I am not the fastest gun in the west.