enable add node from csv
This commit is contained in:
parent
27329ddcb9
commit
721a565e22
56
add_csv.py
Normal file
56
add_csv.py
Normal file
@ -0,0 +1,56 @@
|
||||
import pandas as pd
|
||||
from neo4j import GraphDatabase
|
||||
import argparse
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
def create_neo4j_session(uri, user, password):
|
||||
return GraphDatabase.driver(uri, auth=(user, password))
|
||||
|
||||
def create_node(tx, node_type, properties):
|
||||
# Create a Cypher query to create a node with given type and properties
|
||||
properties_string = ', '.join([f"{key}: '{value}'" for key, value in properties.items()])
|
||||
query = f"CREATE (n:{node_type} {{{properties_string}}})"
|
||||
tx.run(query)
|
||||
|
||||
def main(csv_file_path):
|
||||
# Read environment variables
|
||||
NEO4J_URI = os.getenv("NEO4J_URI")
|
||||
NEO4J_USER = os.getenv("NEO4J_USER")
|
||||
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")
|
||||
|
||||
if not all([NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD]):
|
||||
raise ValueError("Neo4j URI, user, and password must be set in the environment variables.")
|
||||
|
||||
# Read the CSV file into a DataFrame
|
||||
df = pd.read_csv(csv_file_path, delimiter='^', usecols=['type', 'id', 'title', 'description', 'url', 'date_enacted', 'date_repealed'])
|
||||
|
||||
# Connect to Neo4j
|
||||
driver = create_neo4j_session(NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD)
|
||||
|
||||
with driver.session() as session:
|
||||
for _, row in df.iterrows():
|
||||
node_type = row['type']
|
||||
properties = {
|
||||
'id': str(row['id']),
|
||||
'title': row['title'],
|
||||
'description': row['description'],
|
||||
'url': row['url'],
|
||||
'date_enacted': pd.to_datetime(row['date_enacted']).strftime('%Y-%m-%d') if pd.notnull(row['date_enacted']) else None,
|
||||
'date_repealed': pd.to_datetime(row['date_repealed']).strftime('%Y-%m-%d') if pd.notnull(row['date_repealed']) else None
|
||||
}
|
||||
|
||||
# Create the node in Neo4j
|
||||
session.write_transaction(create_node, node_type, properties)
|
||||
|
||||
driver.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Read a CSV file and create nodes in Neo4j.")
|
||||
parser.add_argument("csv_file_path", type=str, help="Path to the CSV file")
|
||||
|
||||
args = parser.parse_args()
|
||||
main(args.csv_file_path)
|
10
executive_orders.csv
Normal file
10
executive_orders.csv
Normal file
@ -0,0 +1,10 @@
|
||||
type^id^title^description^url^date_enacted^date_repealed
|
||||
order^EO 14222^"Implementing the Presidents ""Department of Government Efficiency"" Cost Efficiency Initiative"^^https://www.federalregister.gov/d/2025-03527^February 26, 2025^
|
||||
order^EO 14221^Making America Healthy Again by Empowering Patients With Clear, Accurate, and Actionable Healthcare Pricing Information^^https://www.federalregister.gov/d/2025-03440^February 25, 2025^
|
||||
order^EO 14220^Addressing the Threat to National Security From Imports of Copper^^https://www.federalregister.gov/d/2025-03439^February 25, 2025^
|
||||
order^EO 14219^"Ensuring Lawful Governance and Implementing the Presidents ""Department of Government Efficiency"" Deregulatory Initiative"^^https://www.federalregister.gov/d/2025-03138^February 19, 2025^
|
||||
order^EO 14218^Ending Taxpayer Subsidization of Open Borders^^https://www.federalregister.gov/d/2025-03137^February 19, 2025^
|
||||
order^EO 14216^Expanding Access to In Vitro Fertilization^^https://www.federalregister.gov/d/2025-03064^February 18, 2025^
|
||||
order^EO 14215^Ensuring Accountability for All Agencies^^https://www.federalregister.gov/d/2025-03063^February 18, 2025^
|
||||
order^EO 14214^Keeping Education Accessible and Ending COVID-19 Vaccine Mandates in Schools^^https://www.federalregister.gov/d/2025-02931^February 14, 2025^
|
||||
order^EO 14213^Establishing the National Energy Dominance Council^^https://www.federalregister.gov/d/2025-02928^February 14, 2025^
|
Can't render this file because it contains an unexpected character in line 2 and column 16.
|
3
requirments.txt
Normal file
3
requirments.txt
Normal file
@ -0,0 +1,3 @@
|
||||
pandas
|
||||
neo4j
|
||||
python-dotenv
|
Loading…
Reference in New Issue
Block a user