Skip to content

Instantly share code, notes, and snippets.

@ikbalsingh
Created November 22, 2023 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikbalsingh/d12d43ef868a730442fa07ff39ee0124 to your computer and use it in GitHub Desktop.
Save ikbalsingh/d12d43ef868a730442fa07ff39ee0124 to your computer and use it in GitHub Desktop.
import requests
import gzip
import shutil
import os
def download_and_extract_gz(url):
# Send an HTTP GET request to the URL
response = requests.get(url)
if response.status_code == 200:
# Request was successful, save the content to a local .gz file
with open("higgs-social_network.edgelist.gz", "wb") as file:
file.write(response.content)
# Decompress the .gz file
with gzip.open("higgs-social_network.edgelist.gz", 'rb') as f_in:
with open("higgs-social_network.edgelist", 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
# Remove the downloaded .gz file
os.remove("higgs-social_network.edgelist.gz")
print(f"File extracted and saved.")
else:
print(f"Failed to download the .gz file. Status code: {response.status_code}")
# Usage
download_and_extract_gz("https://snap.stanford.edu/data/higgs-social_network.edgelist.gz")
import networkx as nx
def load_edge_list(file_path):
# Create an empty graph
G = nx.Graph()
# Read the edge list from the file
with open(file_path, 'r') as file:
for line in file:
# Assuming the edge list is space or tab-separated
u, v = map(int, line.strip().split())
G.add_edge(u, v)
return G
# Usage
file_path = 'higgs-social_network.edgelist'
graph = load_edge_list(file_path)
# Now you can use the 'graph' object for various graph-related operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment