-2

そのようなデータを含む複数のレコードがあるファイルがあります

F00DY4302B8JRQ ランク=0000030 x=800.0 y=1412.0 長さ=89

ここで、長さ <= 50 が見つかった場合は、この行とファイル内の次の行を削除して別のファイルに書き込む行を検索します。

みんな、ありがとう

4

3 に答える 3

1

頭のてっぺんから:

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

これがあなたが仕事を始めるために必要なものであることを願っています.

于 2009-12-29T04:09:56.813 に答える
1

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()
于 2009-12-29T04:10:42.523 に答える
0

列が常に同じ順序であり、常に同じ番号がある場合は.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これを書いている間に2つの答えが現れました。私は西部で最速の銃ではありません。

于 2009-12-29T04:12:49.037 に答える