python - 从 python 中的 csv 创建具有唯一属性的唯一节点字典以加载到 neo4j

标签 python csv dictionary neo4j cypher

我很难想出最好的方法来做到这一点;我无法找出字典结构来解析 csv 并创建具有独特属性的独特节点的各种节点字典,然后加载到 Neo4j 中。

这是我到目前为止所拥有的:

import blah blah

driver = GraphDatabase.driver("bolt://localhost")
input_location = "/path/to/file/file.csv"
output_location = "/path/to/dir/"
def main():

origin = {"PORT": [], "NUMBER": [], "CALL_SIGN": []}
ship = {"BOAT_NAME": [], "BOAT_NUMBER": []}
shipper = {"NAME": [], "STREET": [], "CITY": [], "ZIPCODE": []}
destination = {"COUNTRY": [], "CITY": []}
node_type_list = [origin, ship, shipper, destination]

with open(input_location, "rb") as ship_data:
    reader = csv.DictReader(ship_data, delimiter='|')
    print "parsing & uploading data\n"
    for row in reader:
        for node_type in node_type_list:
            for key in node_type:
                dict_load(node_type,key,row[key])
    send_nodes_to_graph(node_type_list)

def dict_load(node_type,key,value):
    try:
        if value not in node_type[key]:
            node_type[key].append(value)
    except Exception as e:
        print e

def send_nodes_to_graph(node_type_list):
    session = driver.session()
    session.run('''something_like_this:http://stackoverflow.com/questions/40332698/can-a-python-dictionary-be-passed-as-neo4j-literal-maps/40333843#40333843''')
    session.close()
if __name__ == '__main__':

    main()

我的 csv 看起来像这样:

COUNTRY NUMBER  CALL_SIGN   PORT    BOAT_NAME   BOAT_NUMBER NAME    STREET  CITY    ST  ZIPCODE
D REP   91487   S DOMINGO   BALTIMORE   PESCADO 1276394 JH FWEICH   9874 LOMBARDO WAY   PORT ELIZABETH  NJ  8348
D REP   91487   S DOMINGO   VA BEACH    TROPIC  9872347 JH FWEICH   9874 LOMBARDO WAY   PORT ELIZABETH  NJ  8348
D REP   91487   S DOMINGO   VA BEACH    TROPIC  9872347 JH FWEICH   9874 LOMBARDO WAY   PORT ELIZABETH  NJ  8348
D REP   91487   S DOMINGO   VA BEACH    CAPRICORN   8761231 JH FWEICH   9874 LOMBARDO WAY   PORT ELIZABETH  NJ  8348

我的字典结构当前产生这个:

origin {'NUMBER': ['91487'], 'CALL_SIGN': ['S DOMINGO'], 'PORT': ['BALTIMORE', 'VA BEACH']}

但我认为它需要看起来更像这样,以便仅将唯一节点加载到 Neo4j 中:

origin {'91487': {'CALL_SIGN': 'S DOMINGO', 'PORT': 'BALTIMORE'}}
origin {'91487': {'CALL_SIGN': 'S DOMINGO', 'PORT': 'VA BEACH'}}
ship {'1276394': {'BOAT_NAME': 'PESCADO'}}
shipper {'JH FWEICH': {'STREET': '9874 LOMBARDO WAY', 'CITY':'PORT ELIZABETH'}} 
etc....

最佳答案

也许尝试这样的事情?

origin = {"PORT", "NUMBER", "CALL_SIGN"}
ship = {"BOAT_NAME", "BOAT_NUMBER"}
shipper = {"NAME", "STREET", "CITY", "ZIPCODE"}
destination = {"COUNTRY", "CITY"}
node_type_list = [origin, ship, shipper, destination]

with open(input_location, "rb") as ship_data:
    reader = csv.DictReader(ship_data, delimiter='|')
    print "parsing & uploading data\n"
    for row in reader:
        dict_list = [{row["NUMBER"]: {key: row[key] for key in sublist}}for sublist in node_type_list]

这会构建一个字典列表,将NUMBER映射到输入字典的一部分。我不熟悉 neo4j,但希望这更符合您的需求。输出应类似于 [{'91487': {'CALL_SIGN': 'S DOMINGO', 'PORT': 'BALTIMORE'}}, {'91487': {'BOAT_NUMBER': '1276394', 'BOAT_NAME' : 'PESCDO'}}, ...]

关于python - 从 python 中的 csv 创建具有唯一属性的唯一节点字典以加载到 neo4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490389/

相关文章:

java - 在将 CSV 字段导入数据库之前修改它们

Python 3 读取带有行换行符的 CSV 文件

javascript - 在 Django 中将字符串转换为 Javascript 中的字典列表

c# - 为什么从字典中获取时会得到 Action<> 的克隆?

python - 我们可以在pyspark的ParamGridBuilder中使用for循环吗?

python - Pandas :每n行的累积总和

python - 处理定义

python - 无法将视频转换为灰度

c - 在 .C 中转置 csv

c++ - 查找函数未返回正确的值