withopen(dict, 'r') as fd, open(input, 'r') as fin, open(output, 'w') as fout: for line in fd: parts = line.rstrip().split('\t') iflen(parts) != 2: print"wrong format line:%s" % line continue dt[parts[0]] = parts[1] for line in fin: parts = line.rstrip().split('\t') iflen(parts) != 3: print"wrong format line:%s" % line continue ideaid = parts[0] material = parts[2] matchs = regex_img.process(material) iflen(matchs) != 3: print"wrong img num:%d" % len(matchs) print matchs continue if ideaid notin dt: print"ideaid:%s has not its planid" % (ideaid) continue tmp_dt = {} tmp_dt["planid"] = dt[ideaid] tmp_dt["img_src"] = matchs json_str = json.dumps(tmp_dt) fout.write("%s\n" % json_str)