You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
# @Time : 2022/6/2 14:02
# @Author : ZhaoXiangPeng
# @File : getkeys.py
from ReSpider.db.redisdb import RedisDB
import pandas as pd
import json
from typing import List, Dict
class GetCount:
def __init__(self, db: int = 1, write_path: str = None):
self.client = RedisDB(db=db)
self.write_path = write_path or 'E:/inspec/'
def get_keys(self) -> List[bytes]:
return self.client.keys(pattern='*-*')
def get_kv(self, key) -> dict:
return self.client.hgetall(key)
@staticmethod
def format(records: dict, key: str) -> list:
temp = []
for k, v in records.items():
record: dict = {'doi': k.decode('utf-8'), 'count': int(v.decode('utf-8')), 'issn': key}
temp.append(record)
return temp
def to_csv(self, data: list = None, file_name: str = None):
df = pd.DataFrame(data)
df.to_csv(self.write_path+file_name+'.csv', index=False)
def aa(self):
redis_keys: List[bytes] = self.get_keys()
for redis_key in redis_keys:
key_string: str = redis_key.decode('utf-8')
kvs: dict = self.get_kv(key_string)
print('*'*5 + key_string + '*'*5)
key_records = self.format(kvs, key_string)
self.to_csv(data=key_records, file_name=key_string)
if __name__ == '__main__':
gc = GetCount(db=2)
# gc.get_keys()
gc.aa()