Skip to content

Instantly share code, notes, and snippets.

@daemonfire300
Last active June 8, 2016 21:11
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 daemonfire300/51f3623799e12c7f84d236449170b473 to your computer and use it in GitHub Desktop.
Save daemonfire300/51f3623799e12c7f84d236449170b473 to your computer and use it in GitHub Desktop.
Little Helper Script to convert CSV files for neo4j
# -*- coding: utf-8 -*-
"""
Created on Wed May 25 11:26:57 2016
@author: julius
"""
import getopt, sys
def getOptionValue(opts, name, is_long=False):
fmt = "-{}"
if is_long:
fmt = "-" + fmt
o = [item for item in opts if item[0] == fmt.format(name)]
if len(o) > 0:
return o.pop()[1]
else:
return None
# Sample usage
# python3 txt_to_neocsv.py -s data/location/wln/wln_label-type.txt -d data/location/wln/wln_label-type.txt.csv -w tab -h "locationid:ID(Location),title:string,locationlabel:string" --stringify=2
def main():
opts, args = getopt.getopt(sys.argv[1:], 's:d:h:w:', longopts=['source_file=', 'destination_file=', 'stringify='])
print(opts)
s_source = getOptionValue(opts, 's')
s_destination = getOptionValue(opts, 'd')
s_header = getOptionValue(opts, 'h')
s_whitespace = getOptionValue(opts, 'w')
i_stringify = int(getOptionValue(opts, 'stringify', is_long=True)) - 1
if s_whitespace == "tab":
s_whitespace = '\t'
if s_whitespace == "ws":
s_whitespace = ' '
print(s_source)
print(s_destination)
print(s_header)
print(i_stringify)
f_src = open(s_source, 'r')
f_dst = open(s_destination, 'w+')
f_dst.writelines([s_header])
next(f_src)
f_dst.write("\n")
for line in f_src:
tmp = line.replace('"', '\\"').split(s_whitespace)
if i_stringify >= 0 and len(tmp) > i_stringify:
tmp[i_stringify] = '"' + tmp[i_stringify] + '"'
tmp = ','.join(tmp)
f_dst.write(tmp)
print("Done")
f_dst.close()
f_src.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment