def write_mongodb(data):
# 正则表达式匹配每一行我们想要的数据
pattern = re.compile(r".*/(\w+),(\w+),(\d+)")
for item in data:
item = item.strip()
d = pattern.match(item)
a_code = d.group(1)
b_code = d.group(2)
batch_no = d.group(3)
user_id = bson.ObjectId("aaabbb")
msg = {"a_code": a_code, "b_code": b_code, "batch_code": batch_code, "batch_no": batch_no,
"created_at": datetime.datetime.now(), "updatead_at": datetime.datetime.now(),
"invaild": True, "invaild_at": datetime.datetime.now(),
"user_id": user_id, "user_name": "test"}
try:
fake_table.insert_one(msg)
except Exception as e:
logging.info(item)
logging.info(e)
pass
if __name__ == '__main__':
# 文件中的内容是 https://info.svnup.cn/A/3ED00646C15F4CF0A110769796DNAC6,22621C99067B4D91BDSDGB3A60702C6D,00000001
file = glob.glob("fake_file_dir/*.txt")
if len(file) != 1:
print("fake_file_dir目录下只能有一个文件而却只能是要导入的那个文件")
exit()
# 从文件中读出数据把每行数据分开放在一个大列表中data
with open(file[0]) as f:
file_data = f.readlines()
print("总共有---> %s <----数据" % len(file_data))
file = os.path.basename(file[0])
batch_code = re.match(r"(.*)\.txt", file)
batch_code = batch_code.group(1)
# 调用write_mongodb函数
write_mongodb(file_data)
网友评论