export_fci.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:fci-breeds 作者: paiv 项目源码 文件源码
def export_from(datadir, tofile='out.csv'):
  data = []
  for (root,dirs,files) in os.walk(datadir):
    for fn in [os.path.join(root,x) for x in files if x == 'entry.json']:
      with open(fn, 'r', encoding='utf-8') as fd:
        entry = json.load(fd)
        data.append(entry)

  data = sorted(data, lambda a,b: int(a['refid']) - int(b['refid']))

  with open(tofile, 'wb') as csvfile:
    writer = csv.DictWriter(csvfile, ['id','name','section','provisional','country','url','image','pdf'])
    writer.writeheader()

    for entry in data:
      for n in ['name', 'section', 'country']:
        entry[n] = unicode(entry[n]).encode('utf-8')
      entry['id'] = entry['refid']
      entry.pop('refid')
      entry['image'] = None
      if 'thumb' in entry:
        entry['image'] = entry['thumb']
        entry.pop('thumb')

      writer.writerow(entry)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号