foods_data.csv
建议值用“”引起来。避免中间有,号造成误识别
饮食
"辣椒"
"大蒜"
"芥末"
"海鲜"
"胡萝卜"
"核桃仁"
"菠菜"
"西红柿"
"香蕉"
重构代码,将 def execute_write(self, cql):
移至 neo4j_provider.py
中
import logging
import pandas as pd
from utils.neo4j_provider import neo4j
logging.root.setLevel(logging.INFO)
# 并生成 CQL
def generate_cql() -> str:
df = pd.read_csv('foods_data.csv')
symptoms = []
for each in df['饮食']:
symptoms.extend(each.split(',')) # 按,号分割成数组,并将每行数据到一个队列里面
symptoms = set(symptoms) # 去除重复项
# 拼接 CQL
cql = ""
for idx, item in enumerate(symptoms):
cql += """(foods%s:Foods {name: "%s"}),\r\n""" \
% (idx, item)
return "CREATE %s" % (cql.rstrip(",\r\n")) # 删除最后一个节点的 逗号
# 清除 Disease 标签数据
def clear_data():
cql = "MATCH (n:Foods) DETACH DELETE n"
neo4j.execute_write(cql)
if __name__ == "__main__":
clear_data()
cql = generate_cql()
print(cql)
neo4j.execute_write(cql)