1

以下のコードで次の問題が発生しています。どこで問題が発生しているのかを入力してください。

  1. change_ignore_base.txtとchange_ignore_file.txtが作成されていませんが、どこで問題が発生していますか?

  2. chagne_ignoreに「\r」と「\n」が追加されているのがわかります。これらを取り除き、後で検索に使用できる変数に入れるための賢い方法は何ですか。

change_ids.txt

206061
150362
147117
147441
143446
200912

change_ignore.txt

150362
147117
147441
143446
200914   

コード

import os
import subprocess
from subprocess import check_call

def sync (base_change):
    # open a file
    with open('change_ignore.txt') as f:
        change_ignore = f.readlines()
        print "change_ignore"
        print change_ignore

    with open('change_ids.txt') as f:
        lines = f.readlines()
        for line in lines:
            line=line.strip()
            print line
            if line <= base_change:
                print "IN line<=base_change"
                print line
                with open("change_ignore_base.txt", "a") as myfile:
                    myfile.write(line)
            if line in change_ignore:
                print "IN change_ignore"
                print line
                with open("change_ignore_file.txt", "a") as myfile:
                    myfile.write("line")
            if line > base_change and line not in change_ignore:
                pass


def main ():
    base_change=200913
    sync(base_change)

if __name__ == '__main__':
    main()
4

1 に答える 1

1

これがあなたのプログラムへの穏やかな調整であり、あなたが望むことを達成すると私は信じています。重要なポイント(コメントで指摘されているように)は、整数と整数を比較したいことと、ファイルを何度も開いたり閉じたりしないようにすることです(ループ内のファイルの追加で発生したように)。

import os
import subprocess
from subprocess import check_call

def sync(base_change):

    # Generate a list of integers based on your change_ignore file
    with open('change_ignore.txt', 'rb') as f:
        # Here we make a list of integers based on the file
        change_ignore = [int(line.strip()) for line in f]

    # Store your hits/misses in lists; that way you do not
    # need to continuously open/close files while appending
    change_ignore_base = []
    change_ignore_file = []

    # Now open the file of the IDs
    with open('change_ids.txt', 'rb') as f:
        # Iterate over the file itself
        for line in f:
            # Convert the line to an integer (note that this
            # implicitly removes the newline characters)
            # However we are going to write 'line' to our list,
            # which will keep the newline (more on that later)
            num = int(line)
            print num

            # Now we are comparing ints with ints
            # I'm assuming the print statements are for debugging,
            # so we offset them with some space, making it so that
            # any relevant hits are indented under a number
            if num <= base_change:
                print "  IN line<=base_change"
                change_ignore_base.append(line)
            if num in change_ignore:
                print "  IN change_ignore"
                change_ignore_file.append(line)
            if num > base_change and num not in change_ignore:
                pass

    # Now that you have lists containing the data for your new files,
    # write them (they already have newlines appended so writelines works)
    # You can use 'with' with two files in this way in Python 2.7+,
    # but it goes over 80 characters here so I'm not a huge fan :)
    with open('change_ignore_base', 'wb') as b, open('change_ignore_file', 'wb') as f:
      b.writelines(change_ignore_base)
      f.writelines(change_ignore_file)


def main ():
    base_change=200913
    sync(base_change)

main()

これにより、ファイルが作成され、次のように出力されます。

206061
150362
  IN line<=base_change
  IN change_ignore
147117
  IN line<=base_change
  IN change_ignore
147441
  IN line<=base_change
  IN change_ignore
143446
  IN line<=base_change
  IN change_ignore
200912
  IN line<=base_change
于 2012-12-28T07:05:01.410 に答える