根据逻辑删除一行

发布于 2024-08-16 02:59:44 字数 185 浏览 5 评论 0原文

我有一个文件,其中有多个包含此类数据的记录

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以笙箫默 2024-08-23 02:59:44

从我的脑海中:

for every line in file
split by spaces
get last token
split by equal
verify length
write line to another file
delete line and the next

希望这是你开始工作所需要的。

From the top of my head:

for every line in file
split by spaces
get last token
split by equal
verify length
write line to another file
delete line and the next

Hope this is what you need to start working.

少女情怀诗 2024-08-23 02:59:44

假设 Python 2.6(请告诉我们您是否需要另一个版本!),并且您希望跳过长度 <= 50 的每一行(并忽略每种情况下的下一行)(如果有) :

import re

def weirdtask(infname, oufname):
  inf = open(infname, 'r')
  ouf = open(oufname, 'w')
  rle = re.compile(r'length=\s*(\d+)')
  for line in inf:
    mo = re.search(line)
    if mo:
      thelen = int(mo.group(1))
      if thelen <= 50:
        next(inf)
        continue
    ouf.write(line)
  ouf.close()

如果这不完全是您的规格,请澄清。

  inf.close()

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:

import re

def weirdtask(infname, oufname):
  inf = open(infname, 'r')
  ouf = open(oufname, 'w')
  rle = re.compile(r'length=\s*(\d+)')
  for line in inf:
    mo = re.search(line)
    if mo:
      thelen = int(mo.group(1))
      if thelen <= 50:
        next(inf)
        continue
    ouf.write(line)
  ouf.close()

If that's not exactly your specs, please clarify.

  inf.close()
扛刀软妹 2024-08-23 02:59:44

如果列总是以相同的顺序并且总是有相同的数字,您可以对字符串使用 .split() 方法,并通过索引找到您想要的列

words = line.split()
l = words[4]
temp = l.split("=")[2]
if int(temp) <= 50:
    # found the line, handle it
    do_something_here()

:列可以按任何顺序排列,您可以使用正则表达式。

s_pat = "length\s*=\s*(\d+)"
pat = re.compile(s_pat)

m = pat.search(line)
if m:
    temp = m.group(1)
    if int(temp) <= 50:
        # found the line, handle it
        do_something_here()

这使用正则表达式中的“匹配组”来获取数字。

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:

words = line.split()
l = words[4]
temp = l.split("=")[2]
if int(temp) <= 50:
    # found the line, handle it
    do_something_here()

If the columns might be in any order, you could use regular expressions.

s_pat = "length\s*=\s*(\d+)"
pat = re.compile(s_pat)

m = pat.search(line)
if m:
    temp = m.group(1)
    if int(temp) <= 50:
        # found the line, handle it
        do_something_here()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文