CategoriesOffice Space

D2L to CSV

Well, sometimes there comes a point where you want some data off Desire to Learn (D2L) and have it in CSV format. This is especially true for surveys, which typically need to be evaluated using other tools. A simple PERL script can help in the process…feel free to use/modify this little script as you need – credit always welcome 😉

[perl]
use strict;
use warnings;

open CSVFILE, “<$ARGV[0]” or die $!;
my %questions;
my $capture = 0;
my $user = 0;
my $lastnum = 1;
while (<CSVFILE>) {
my @line = split(“,”, $_);
if ($line[0] =~ /^User/) {
$capture = 1;
my @userd = split(” “, $line[0]);
$user = $userd[1];
}
next if !$capture;

my $num = $line[1]; # question number – might have multiple entries for one!
if ($line[2] eq “LIK” ) {
chomp($line[$#line – 2]);
$line[$#line – 2] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 2] =~ s/”+//g;
$line[$#line – 2] =~ s/^s+//;
$line[$#line – 2] =~ s/s+$//;
if ((($line[$#line-1] eq “Strongly Disagree”) || ($line[$#line-1] eq “Never”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 1 ;
}
elsif ((($line[$#line-1] eq “Disagree”) || ($line[$#line-1] eq “Rarely”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 2 ;
}
elsif ((($line[$#line-1] eq “Neutral”) || ($line[$#line-1] eq “Occasionally”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 3 ;
}
elsif ((($line[$#line-1] eq “Agree”) || ($line[$#line-1] eq “Regularly”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 4 ;
}
elsif ((($line[$#line-1] eq “Strongly Agree”) || ($line[$#line-1] eq “Always”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 5 ;
}
elsif ((($line[$#line-1] eq “N/A”)) && ($line[$#line] == 1)) {
$questions{$user}{$num}{$line[$#line – 2]} = 0 ;
}
}
elsif ($line[2] eq “M-S” ) {
chomp($line[$#line – 2]);
$line[$#line – 2] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 2] =~ s/”+//g;
$line[$#line – 2] =~ s/^s+//;
$line[$#line – 2] =~ s/s+$//;
if ($line[$#line] == 1) {
$questions{$user}{$num}{$line[$#line – 2]} = 1 ;
}
else {
$questions{$user}{$num}{$line[$#line – 2]} = 0 ;
}
}
elsif ($line[2] eq “MC” ) {
chomp($line[$#line – 2]);
$line[$#line – 2] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 2] =~ s/”+//g;
$line[$#line – 2] =~ s/^s+//;
$line[$#line – 2] =~ s/s+$//;
if ($line[$#line] == 1) {
$questions{$user}{$num}{$line[$#line – 2]} = 1 ;
}
}
elsif ($line[2] eq “FIB” ) {
chomp($line[$#line – 1]);
$line[$#line – 1] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 1] =~ s/”+//g;
$line[$#line – 1] =~ s/^s+//;
$line[$#line – 1] =~ s/s+$//;

chomp($line[$#line – 2]);
$line[$#line – 2] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 2] =~ s/”+//g;
$line[$#line – 2] =~ s/^s+//;
$line[$#line – 2] =~ s/s+$//;

$questions{$user}{$num}{$line[$#line – 2]} = $line[$#line – 1] ;
}
elsif ($line[2] eq “SA” ) {
chomp($line[$#line – 1]);
$line[$#line – 1] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[$#line – 1] =~ s/”+//g;
$line[$#line – 1] =~ s/^s+//;
$line[$#line – 1] =~ s/s+$//;

chomp($line[4]);
$line[4] =~ s/<(.*?)>//gi; # Strip HTML from string
$line[4] =~ s/”+//g;
$line[4] =~ s/^s+//;
$line[4] =~ s/s+$//;

$questions{$user}{$num}{$line[4]} = $line[$#line – 1] ;
}

}
close CSVFILE;

foreach my $user (sort {$a <=> $b} keys %questions) {
print “User, “;
foreach my $qnum (sort {$a <=> $b} keys %{$questions{$user}}) {
foreach my $q (keys %{$questions{$user}{$qnum}}) {
print “Q$qnum:$q, “;
}
}
print “n”;
last;
}

foreach my $user (sort {$a <=> $b} keys %questions) {
print “$user, “;
foreach my $qnum (sort {$a <=> $b} keys %{$questions{$user}}) {
foreach my $q (keys %{$questions{$user}{$qnum}}) {
print $questions{$user}{$qnum}{$q} . “,”;
}
}
print “n”;
}

exit;
[/perl]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.