Skip to content

Instantly share code, notes, and snippets.

@JJ
Created October 24, 2011 12:29
Show Gist options
  • Save JJ/1308912 to your computer and use it in GitHub Desktop.
Save JJ/1308912 to your computer and use it in GitHub Desktop.
Convert CSV files to Pajek .net format. Takes file with two columns as argument; every row indicates a connection from agent in column A to agent in Column B. Outputs a file with the same root and .net extension in .net format, which can be used in Pajek
#!/usr/bin/perl
use strict;
use warnings;
use File::Slurp qw(read_file);
my $csv = shift || die "No defaults; $0 <csvfile>\n";
my $content = read_file($csv) || die "Can't read file $csv: $!\n";
my %pairings;
my %players;
for my $l ( split("\n", $content )) {
my ($one, $the_other) = split(/\s+/, $l );
$pairings{$one}{$the_other}++;
$players{$one}++;
$players{$the_other}++;
}
my ($file_root) = ($csv =~ /(.+)\./);
open my $out_file, ">", "$file_root.net";
say $out_file "*Vertices ", scalar keys %players;
my @these_keys = keys %players;
for ( my $i = 0; $i <= $#these_keys; $i++ ) {
say $out_file $i+1, " \"$these_keys[$i]\"";
}
say $out_file "*arcs";
for ( my $i = 0; $i <= $#these_keys; $i++ ) {
for ( my $j = 0; $j <= $#these_keys; $j++ ) {
if ( $pairings{$these_keys[$i]}{$these_keys[$j]} ) {
say $out_file $i+1, " ", $j + 1, " ",
$pairings{$these_keys[$i]}{$these_keys[$j]};
}
}
}
close $out_file;
@mohsin36295
Copy link

mohsin36295 commented Sep 16, 2017

Is there any code available which can also add time stamp with edges so we can transform network into time ? basically saying i want to convert csv file into pajek time event network

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