/[virtual-ldap]/bin/csv2yaml.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /bin/csv2yaml.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (show annotations)
Mon Mar 16 13:54:50 2009 UTC (10 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 1399 byte(s)
encode yaml in utf-8
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 # cvs2dbi.pl
7 #
8 # 03/15/2009 05:06:53 PM CET Dobrica Pavlinusic <dpavlin@rot13.org>
9
10 use Data::Dump qw/dump/;
11 use File::Slurp;
12 use YAML qw/DumpFile/;
13 use Text::CSV;
14 use Encode qw/from_to/;
15
16 my $debug = 0;
17
18 my $path = shift @ARGV || die "usage: $0 file.csv\n";
19
20 my $csv = read_file( $path );
21 from_to($csv, 'utf-16', 'utf-8');
22
23 my @columns;
24
25 foreach my $line ( split(/\r?\n/, $csv) ) {
26
27 warn "## $line\n";
28
29 my @fields = split(/;/, $line);
30 if ( ! @columns ) {
31 @columns = @fields;
32 warn "# columns = ",dump( @columns ) if $debug;
33 next;
34 }
35
36 my $hash;
37
38 warn "# fields = ",dump( @fields ) if $debug;
39
40 foreach ( 0 .. $#fields ) {
41 my $n = $columns[$_];
42 my $v = $fields[$_];
43
44 $v =~ s{\s*#\s*$}{};
45 $v =~ s{^\s+}{};
46 $v =~ s{\s+$}{};
47
48 # fix tel fields
49 $v =~ s{\s+}{#}g if $n =~ m{tel};
50 $v =~ s[\x{17d}][F] if $n =~ m{spol};
51
52 if ( $v =~ m{#} ) { # subfields delimiter in CSV data
53 my @v = split(/\s*#+\s*/, $v);
54 foreach my $pos ( 0 .. $#v ) {
55 if ( $n =~ m{tel} ) {
56 if ( $v[$pos] =~ m{^09} ) {
57 $hash->{ $n . '_mobile' } ||= $v[$pos];
58 } else {
59 $hash->{ $n . '_fixed' } ||= $v[$pos];
60 }
61 }
62 $hash->{ $n . '_' . $pos } = $v[$pos];
63 }
64
65 $hash->{ $n } = [ @v ];
66 } else {
67 $hash->{ $n } = $v;
68 }
69 }
70
71 warn dump( $hash ) if $debug;
72
73 my $uuid = $fields[0];
74
75 DumpFile( "yaml/$uuid.yaml", $hash );
76 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26