1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/sql-graph/csv_as_node.py
Estienne Granet 0ff1ae74a4 Adding graph examples (#258)
* Adding graph examples

* Updating README.md
2017-07-14 13:11:16 -07:00

47 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
import os
from optparse import OptionParser
def main(input_file_path, schema, table):
# create the output file in the same directory as the input file
output_file_path = os.path.splitext(input_file_path)[0] + '_as_node' + '.csv'
# you may have to change the encoding
with open(input_file_path, mode='r', encoding = 'utf-16le', newline='') as input_file, \
open(output_file_path, mode='w', encoding = 'utf-16le', newline='') as output_file:
line = input_file.readline()
line_number = 0
# read each line of the input file, add a $node_id column and write down the result on the output file
while line:
if line_number == 0:
newline = '\ufeff' + '{"type":"node","schema":"' + schema + '","table":"' + table + '","id":' + str(line_number) + '}\t' + line[1:]
else:
newline = '{"type":"node","schema":"' + schema + '","table":"' + table + '","id":' + str(line_number) + '}\t' + line
output_file.write(newline)
line_number += 1
line = input_file.readline()
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-f", "--file", action="store", type="string", dest="input_file_name")
parser.add_option("-s", "--schema", action="store", type="string", dest="schema")
parser.add_option("-t", "--table", action="store", type="string", dest="table")
(options, args) = parser.parse_args()
# retrieve options if not provided by the user
if (options.input_file_name == None):
options.name = input('Please enter the full path to the csv file you will import as a node table:')
if (options.schema == None):
options.name = input('Please enter the SQL schema of the node table you will populate:')
if (options.table == None):
options.name = input('Please enter the SQL name of the node table you are populate:')
main(options.input_file_name, options.schema, options.table)