Skip to content

Instantly share code, notes, and snippets.

@dsignr
Last active November 23, 2022 20:09
Show Gist options
  • Save dsignr/c3f7a67fcfb1fb93698a507f4cce8eef to your computer and use it in GitHub Desktop.
Save dsignr/c3f7a67fcfb1fb93698a507f4cce8eef to your computer and use it in GitHub Desktop.
A python script to extract data from CSV and convert it into Gephi compatible GML.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@TarekHammad337
Copy link

xml2csv

import sys, time
import pandas as pd
import datetime as dt
from IPython.display import display

import csv
import xml.etree.ElementTree as ET

def xml_to_csv(file_path,csv_name) -> None:
tree = ET.parse(file_path)
root = tree.getroot()

with open(csv_name, 'w') as csv_file:
    writer= csv.writer(csv_file)
    headers = (child.tag for child in root[0])
    writer.writerow(headers)
    num_records = len(root)
    
    for record in range(num_records):
        rec = (child.text for child in root[record])
        writer.writerow(rec)

if name = 'main':

 import sys
 import pathlib


try:
    file_path= sys.argv[1]
    csv_name=  sys.argv[2]
    
except IndexError:
    sys.exit('Tow argument required. One cml path and one save file name.')
        
with pathlib.Path(file_path) as xml_file:
    if xml_file.is_file():
        xml_to_csv(file_path, csv_name)
        
        
    else:
        sys.exit(f'Did not find {file_path}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment