在匹配字符串后处理文件

时间:2017-12-06 17:10:02

标签: python json python-3.x

我有一个json文件我正在做一些处理,我不得不暂停操作,我想再次开始处理我离开的地方,这是我正在使用的代码,问题是If条件仍然执行如果不满足While循环条件,根据我的理解,代码应该无法访问,直到While循环不符合它的条件。我哪里错了?

row_counter = 0
start_now = 0
Finished = 0
with open('jsonfile') as f:
    for row in f:
        row_counter += 1
        if row_counter > start_now:
            try:
                row = json.loads(row)
                comment_id = row['id']

                while Finished == 0:
                    if (comment_id != 'The_String_I_Wanna_Match'):
                        Finished = True
                        break

                if (condition):
                    print('code i want to execute')
            except Exception as e:
                print(e)
        if (row_counter % 100000 == 0):
            print('no. of rows gone by {}'.format(row_counter))

2 个答案:

答案 0 :(得分:1)

所以我不确定你在这里尝试做什么,因为你发布了大部分伪代码,但你肯定不想使用while循环。如果第一行恰好匹配您正在寻找的字符串,那么while循环会锁定您的程序(除非您运行的是多线程,但您似乎并不是这样)。我也认为你在那里使用了错误的条件运算符,但正如我所说,并非100%确定你在这里会发生什么。

这是我认为您需要的:

import json

row_counter = 0
start_now = 0

with open('file.json') as f:
  for row in f:
    row_counter += 1
    if row_counter > start_now:
      try:
        # check to ensure we're not messing with a blank row
        if not row: continue

        # load the row. This line might throw an exception if the 
        # line isn't perfect json
        row = json.loads(row)
        print(row)
        comment_id = row['id']

        if(comment_id == 'The_String_I_Wanna_Match'):
          print('Execute code here')

      except Exception as e:
        import traceback
        print(traceback.print_exc())
    if (row_counter % 100000 == 0):
      print('no. of rows gone by {}'.format(row_counter))

答案 1 :(得分:1)

这应该有用。

Range(DataColumn & 1)