将JSON字符串存储在csv文件中以进行neo4j导入

时间:2019-02-14 19:03:15

标签: python json neo4j

我需要在JSON文件的一个字段中存储一个csv字符串,该文件将用于用neo4j创建一个neo4j-admin import数据库。在生成所有必要的csv文件并尝试创建数据库后,它告诉我没有有效的--nodes文件。我怀疑这是专门在csv中使用存储的JSON字符串引用的问题。这是我用于生成csv文件的代码:

with open(cl_file,'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_ALL)
        writer.writerow(title_list)
        for row in unique_cl_data:
            writer.writerow([row[0], row[1], row[2], row[3], 'Cluster', dataset_name])

JSON字符串存储在row[3]值中,如下所示:

'{"mature_neuron":0.493694929,"intermediate_progenitor_cell":0.0982259823,"immature_neuron":0.1773570713,"glutamatergic_neuron":0.6074802751,"gabaergic_neuron":0.2685863644,"dopaminergic_neuron":0.0234599396,"serotonergic_neruon":0.001022236,"cholinergic_neuron":0.0273108961,"neuroepithelial_cell":0.2173953827,"radial_glia":0.2758471756,"microglia":0.0282818013,"macrophage":0.0,"astrocyte":0.3250249223,"oligodendrocyte_precursor_cell":0.4788073089,"mature_oligodendrocyte":0.3684283806,"schwann_cell_precursor":0.2158159088,"myelinating_schwann_cell":0.3282158992,"nonmyelinating_schwann_cell":0.4526564331,"endothelial_cell":0.7830818309,"mural_cell":0.0756233339}'

生成的csv如下所示:

"clusterId:ID","chartType","clusterName","assign",":LABEL","DATASET"
"scid_engram_fear_traned_tsne_1","tsne","1","{""mature_neuron"":0.793159869,""intermediate_progenitor_cell"":0.000454013,""immature_neuron"":0.0548508584,""glutamatergic_neuron"":1.0792403847,""gabaergic_neuron"":0.3181778459,""dopaminergic_neuron"":0.150589103,""serotonergic_neruon"":0.0096765336,""cholinergic_neuron"":0.0251700647,""neuroepithelial_cell"":0.0594110346,""radial_glia"":0.1539441058,""microglia"":0.0224593362,""macrophage"":0.0300658893,""astrocyte"":0.0996221719,""oligodendrocyte_precursor_cell"":0.0051255739,""mature_oligodendrocyte"":0.0223153229,""schwann_cell_precursor"":0.029507684,""myelinating_schwann_cell"":0.0360644031,""nonmyelinating_schwann_cell"":0.4626932582,""endothelial_cell"":0.0006433937,""mural_cell"":0.0}","Cluster","scid_engram_fear_traned"

可以看出,JSON字符串的键周围有双引号。我怀疑这是问题所在,但不确定。如果是导致导入失败的原因,我不知道如何避免这样的报价。 csv.QUOTE_ALL一直在为我工作(在我尝试存储JSON字符串之前)。

1 个答案:

答案 0 :(得分:0)

最终,我只是使用以下命令替换了row[3]中的某些字符,但效果很好(相同的QUOTE_ALL):

row[3].replace('"', '\\"').replace('\n', '\\n')

在阅读前端字段时,我需要用后面的字符代替

JSON.parse(jsonStr.replace(/\\"/g, '"'))