Created
October 24, 2011 12:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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