[EM] San Francisco IRV Raw Ballot Data

bql at bolson.org bql at bolson.org
Wed Mar 9 13:43:46 PST 2005


On Wed, 9 Mar 2005, Eric Gorr wrote:

> Anyone know if it is still available on an official site somewhere?
> Has anyone happened to have converted it into the form a>b>c, etc...?

http://www.sfgov.org/site/election_index.asp?id=20234

Attached is a perl script to transform the raw data into the form:
NAME ONE>NAME TWO>NAME THREE

Brian Olson
http://bolson.org/
-------------- next part --------------
#!/usr/bin/perl -w
$candidateFileName = "Candidates.txt";
$contestFileName = "Contest.txt";
$precinctFileName = "precinctindex.txt";
$dataFileName = "Details.txt";

%candidates = ();
%contests = ();
%precincts = ();

open( FIN, $candidateFileName );
while ( $line = <FIN> ) {
  $line =~ s/[\r\n]*$//g;
  ($num,$cand) = $line =~ /^([^,]+),(.*)$/;
  if ( $num && $cand ) {
    $candidates{$num} = $cand;
#    print STDERR "num=\"$num\" cand=\"$cand\"\n";
  }
}
close FIN;

open( FIN, $contestFileName );
while ( $line = <FIN> ) {
  $line =~ s/[\r\n]*$//g;
  ($num,$cand) = $line =~ /^([^,]+),(.*)$/;
  if ( $num && $cand ) {
    $contests{$num} = $cand;
  }
}
close FIN;

open( FIN, $precinctFileName );
while ( $line = <FIN> ) {
  $line =~ s/[\r\n]*$//g;
  ($num,$cand) = $line =~ /^([^,]+),(.*)$/;
  if ( $num && $cand ) {
    $precincts{$num} = $cand;
  }
}
close FIN;

%confiles = ();
%bogusCandidates = ();

open FIN, $dataFileName;
while ( $line = <FIN> ) {
  $line =~ s/[\r\n]*$//g;
  ($pre,$ranks) = $line =~ /^(\d\d\d\d)\s*(.*)$/;
  if ( $pre && $ranks ) {
    ($race) = $ranks =~/^(\d\d\d\d)/;
    if ( $race ) {
      my $cf;
      @va = split /\s+/,$ranks;
      if ( (! @va) || ($#va < 0) ) {
	print STDERR "no va from line \"$line\"\n";
	exit 1;
      }
      @vot = ();
      foreach $v ( @va ) {
	my $vt = $candidates{$v};
	if ( ! $vt ) {
#	  print STDERR "bogus candidate \"$v\"\n";
	  if ( 1 ) {
	    if ( $bogusCandidates{$v} ) {
	      $bogusCandidates{$v}++;
	    } else {
	      $bogusCandidates{$v} = 1;
	    }
	  } else {
	    ($vt) = $v =~ /^\d\d\d\d-(.*)/;
	    if ( ! $vt ) {
	      print STDERR "bogus candidate \"$v\"\n";
	      $vt = $v;
	    }
	  }
	} else {
	  push @vot, $vt;
	}
      }
#      @vot = map { $candidates{$_} } @va;
      if ( (! @vot) || ($#vot < 0) ) {
#	print STDERR "no vot from line \"$line\"\n";
#	exit 1;
      } else {
      if ( 0 ) {
      print STDERR<<EOF;
race: $race
ranks: $ranks
va: @va
vot: @vot
EOF
}
      $cf = $confiles{$race};
      if ( ! $cf ) {
	open $cf, ">$race";
	$confiles{$race} = $cf;
      }
      print $cf join( ">", @vot ) . "\n";
      }
    } else {
      print STDERR "bogus ranks in line \"$line\"\n";
    }
  }
}
foreach $cf ( values %confiles ) {
  close $cf;
}
open FOUT, ">report.txt";
foreach $v ( sort keys %bogusCandidates ) {
  print FOUT "$v=$bogusCandidates{$v}\n";
}
close FOUT;


More information about the Election-Methods mailing list