Skip to content

Instantly share code, notes, and snippets.

@erwinvaneyk
Created November 10, 2013 21:48
Show Gist options
  • Save erwinvaneyk/7404414 to your computer and use it in GitHub Desktop.
Save erwinvaneyk/7404414 to your computer and use it in GitHub Desktop.
Script to fix some inconsistencies in self-generated historical stockdata-files. The first argument specifies the relative file-path, next argument(s) trigger the timestamp to datetime-conversion
# Fixes some inconsistencies in self-generated historical stockdata-files.
import sys, datetime;
# Retrieve file arg
try:
path = sys.argv[1]
except:
exit("No file argument provided!");
# Fix data
file = open(path, "r+");
nfile = open("temp." + str(path),"w");
prevline = "";
for line in file:
columns = line.split(",");
# Delete double entries
if line == prevline: continue;
try:
# Convert timestamp to date
if len(sys.argv) > 2:
columns[0] = datetime.datetime.fromtimestamp(int(columns[0])/1e6).strftime("%Y-%m-%d %H:%M:%S");
except Exception as e:
pass
nfile.write(str(",".join(columns)));
prevline = line;
# Clean up
nfile.close();
file.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment