Skip to content

Instantly share code, notes, and snippets.

@nrrb
Created March 21, 2014 02:14
Show Gist options
  • Save nrrb/9678184 to your computer and use it in GitHub Desktop.
Save nrrb/9678184 to your computer and use it in GitHub Desktop.
A simple script to read an edgelist CSV file (each row is "vertex1,vertex2,value") and write to another edgelist file only the rows with non-zero values.
import csv
import sys
if __name__=="__main__":
if len(sys.argv) < 3:
print 'Syntax:'
print 'python {0} edgelist.csv output.csv'.format(__file__)
sys.exit(1)
input_row_count, output_row_count = 0, 0
input_filename, output_filename = sys.argv[1:3]
with open(input_filename, 'rb') as input_file:
with open(output_filename, 'wb') as output_file:
csv_out = csv.writer(output_file)
for row in csv.reader(input_file):
input_row_count += 1
if row[-1] != '0':
csv_out.writerow(row)
output_row_count += 1
print 'Read {0} rows from {1}.'.format(input_row_count, input_filename)
print 'Wrote {0} rows to {1}.'.format(output_row_count, output_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment