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 回答