🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #7729

Re: "Transponer" u fichero de texto.

#7729
From: Sergi Faber
Date: Tue, 11 Feb 2003 23:56
60 lines
2284 bytes
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Han Solo wrote:

> tengo un fichero de texto, supongamos algo como:
> 
> a1 a2 a3 a4 a5
> b1 b2 b3 b4 b5
> c1 c2 c3 c4 c5
> d1 d2 d3 d4 d5
> e1 e2 e3 e4 e5
> 
> y quiero convertirlo en su "transpuesto";
> 
> a1 b1 c1 d1 e1
> a2 b2 c2 d2 e2
> a3 b3 c3 d3 e3
> a4 b4 c4 d4 e4
> a5 b5 c5 d5 e5
[...] 
> Estoy programando un script en perl, pero me está quedando de un
> guarro que asusta.

Bueno, en Perl ni idea, pero si quieres darle una oportunidad a Python, en 7 
líneas lo tienes listo:

Supongamos que tienes el fichero foriginal.txt y quieres obtener ftrans.txt

$ python transpo.py foriginal.txt > ftrans.txt

transpo.py-----------------------------
import sys
f=open(sys.argv[1])
L=f.readlines()
f.close()
L2=[l.split() for l in L]      
L3=zip(*L2)
for l in L3: print " ".join(l)
- ----------------------------------------

Con la versión estable actual (2.2.2) funciona perfectamente ;-)

Salut!

- -- 
Sergi Faber
Linux User #157835
GnuPG Pub Key 0x91DC7713 a pgp.escomposlinux.org
Slackware 8.1 -> n'hi ha d'altres?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+SY2sTcpsM5HcdxMRAsKaAJ4xOyRdC46Rk07T2DhOZSM2xnFBaACfVGLK
ZSyXX0g6VwSbHysFQ3mGBRk=
=9lD1
-----END PGP SIGNATURE-----
Technical Details
Message-ID: <b2c2k0$1an53a$1@ID-116047.news.dfncis.de>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!110.red-80-37-186.pooles.rima-tde.NET!not-for-mail
References: <873cn0hz9a.fsf@ietcc.csic.es>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f86c8,197d58af93fd12d4
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2003-02-11 15:56:50 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!fu-berlin.de!uni-berlin.de!110.red-80-37-186.pooles.rima-tde.NET!not-for-mail
From: Sergi Faber <sergifaber.treuaixo@telefonica.net>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: "Transponer" u fichero de texto.
Date: Wed, 12 Feb 2003 00:56:14 +0100
Organization: FIR
Lines: 60
Message-ID: <b2c2k0$1an53a$1@ID-116047.news.dfncis.de>
References: <873cn0hz9a.fsf@ietcc.csic.es>
NNTP-Posting-Host: 110.red-80-37-186.pooles.rima-tde.net (80.37.186.110)
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: quoted-printable
X-Trace: fu-berlin.de 1045007809 44799082 80.37.186.110 (16 [116047])
User-Agent: KNode/0.7.1