forked from AkshayAgarwal007/Jekyll-Mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2008-04-10-riordinamento-delle-celle-di-un-file.html
19 lines (14 loc) · 3.17 KB
/
2008-04-10-riordinamento-delle-celle-di-un-file.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---
layout: post
title: Riordinamento delle celle di un file CSV
date: '2008-04-10T17:58:00.001+02:00'
author: Luca Ferrari
tags:
- perl
modified_time: '2008-04-10T18:04:08.759+02:00'
blogger_id: tag:blogger.com,1999:blog-1836481905487384887.post-7183535472107972948
blogger_orig_url: http://fluca1978.blogspot.com/2008/04/riordinamento-delle-celle-di-un-file.html
permalink: /:year/:month/:day/:title.html
---
<h1>~</h1>
Quando si esportano/importano dati con file CSV può essere comodo rigirare le celle di una medesima riga ordinandole. Il riordinamento ovviamente ha senso se le celle rappresentano la stessa caratteristica, ossia appartengono alle stessa categoria (es. attributi di classificazione). Il seguente script è un semplice esempio di come ottenere il riordinamento di un file CSV:<br /><br /><pre>#!/usr/bin/perl<br /><br /># Script per ordinare la riga di un file csv. Accetta in ingresso il file di uscita e stampa su<br /># STDOUT le righe riordinate.<br /><br /># * Copyright (C) Luca Ferrari 2008<br /># *<br /># * This program is free software: you can redistribute it and/or modify<br /># * it under the terms of the GNU General Public License as published by<br /># * the Free Software Foundation, either version 3 of the License, or<br /># * (at your option) any later version.<br /># *<br /># * This program is distributed in the hope that it will be useful,<br /># * but WITHOUT ANY WARRANTY; without even the implied warranty of<br /># * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br /># * GNU General Public License for more details.<br /># *<br /># * You should have received a copy of the GNU General Public License<br /># * along with this program. If not, see <http://www.gnu.org/licenses/>.<br /><br /><br />$INPUT_FIELD_SEPARATOR = ";";<br /><br />open(FILE_IN, "<$ARGV[0]") || die("\nImpossibile aprire il file di input $ARGV[0]\n$!\n");<br /><br />while( $line = <file_in> ){<br /> $line =~ s/\r\n//;<br /> $line =~ s/\n//;<br /> @parts = split(";", $line);<br /> <br /><br /> foreach $element (sort (@parts)){<br /> print "$element;";<br /> }<br /><br /> print "\n";<br />}<br /></pre><br /><br /><br />Come si può notare lo script è molto semplice: come primo argomento viene specificato il file CSV da leggere, e ogni riga viene riportata ordinata su standard output (quindi è necessaria la redirezione per l'utilizzo). Da notare che vengono utilizzate due espressioni regolari per la cancellazione dei caratteri di andata a capo; questo è dovuto ad un problema di <span style="font-style: italic;">chomp</span> qualora il file di origine sia stato generato da un computer Microsoft Windows. In questo caso, infatti ogni riga viene terminata da una sequena <span style="font-style: italic;">\r\n</span>, della quale <span style="font-style: italic;">chomp</span> rimuove solo l'ultimo <span style="font-style: italic;">\n</span>. Ne consegue che rimane un ritorno di carrello (<span style="font-style: italic;">\r</span>) che produce sovrascritture dei campi di uscita. La prima espressione regolare verrà utilizzata nel caso di file di origine prodotti da Microsoft Windows, mentre la seconda nel caso di un file prodotto su piattaforma Unix.