473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How can i do this in Python?

perl -ane "print join(qq(\t),@F[0,1,20,21,2,10,12,14,11,4,5,6]).qq(\n)"
file.txt

-a autosplit mode with -n or -p (splits $_ into @F)
-n assume "while (<>) { ... }" loop around program

Jan 25 '07 #1
5 1147
Well, the python interpreter has no equivalent to -n or -a in the perl
interpreter. You'd have to implement it using an actual while loop and
because of the indention sensitivity in Python, I don't believe you can
do it on the command line. (I may be very wrong here, but I've not
gotten it to work).

--
Jerry

On Jan 24, 10:25 pm, "NoName" <zaz...@gmail.comwrote:
perl -ane "print join(qq(\t),@F[0,1,20,21,2,10,12,14,11,4,5,6]).qq(\n)"
file.txt

-a autosplit mode with -n or -p (splits $_ into @F)
-n assume "while (<>) { ... }" loop around program
Jan 25 '07 #2
At Thursday 25/1/2007 01:25, NoName wrote:
>perl -ane "print join(qq(\t),@F[0,1,20,21,2,10,12,14,11,4,5,6]).qq(\n)"
Must be done on a single line?
I'm not sure if I've got right the behavior - it's been some time
since I quit writing Perl code. The script iterates over all lines
contained on all files specified on the command line; for each line,
splits it on whitespace; then it prints a selected set of fields
(columns 0, 1, 20, 21...) using a tab character as field separator.

=== cut ===
import operator,fileinput

mapper = map(operator.itemgetter, [0,1,20,21,2,10,12,14,11,4,5,6])
for line in fileinput.input():
fields = line.split()
print '\t'.join(m(fields) for m in mapper)
=== cut ===
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 25 '07 #3

Thanks;)

On 25 Янв., 17:47, Gabriel Genellina <gagsl...@yahoo.com.ar>wrote:
At Thursday 25/1/2007 01:25, NoName wrote:
perl -ane "print join(qq(\t),@F[0,1,20,21,2,10,12,14,11,4,5,6]).qq(\n)"Must be done on a single line?
I'm not sure if I've got right the behavior - it's been some time
since I quit writing Perl code. The script iterates over all lines
contained on all files specified on the command line; for each line,
splits it on whitespace; then it prints a selected set of fields
(columns 0, 1, 20, 21...) using a tab character as field separator.

=== cut ===
import operator,fileinput

mapper = map(operator.itemgetter, [0,1,20,21,2,10,12,14,11,4,5,6])
for line in fileinput.input():
Â* Â* Â*fields = line.split()
Â* Â* Â*print '\t'.join(m(fields) for m in mapper)
=== cut ===

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. DescubrÃ*.
Todo lo que querÃ*as saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!http://www.yahoo.com.ar/respuestas
Jan 25 '07 #4
Gabriel Genellina:
import operator,fileinput
mapper = map(operator.itemgetter, [0,1,20,21,2,10,12,14,11,4,5,6])
for line in fileinput.input():
fields = line.split()
print '\t'.join(m(fields) for m in mapper)
I'm just curious, what's the advantage of using itemgetter there
compared to something simpler like this (untested!)?

import fileinput
positions = [0,1,20,21,2,10,12,14,11,4,5,6]
for line in fileinput.input():
fields = line.split()
print '\t'.join(fields[m] for m in positions)

Bye,
bearophile

Jan 25 '07 #5
<be************@lycos.comescribió en el mensaje
news:11**********************@v33g2000cwv.googlegr oups.com...
I'm just curious, what's the advantage of using itemgetter there
compared to something simpler like this (untested!)?
None!

--
Gabriel Genellina
Jan 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Olaf Meyer | last post by:
I'm having some problems compiling Python 2.3.3 on HP-UX (B.11.00). I've tried sevral different options for the configure script (e.g. enabling/disabling gcc, aCC) but I always get the same problem...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 259 open ( -5) / 2573 closed (+17) / 2832 total (+12) Bugs : 745 open ( +0) / 4405 closed (+21) / 5150 total (+21) RFE : 150 open...
1
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 241 open ( -6) / 2622 closed (+26) / 2863 total (+20) Bugs : 764 open ( +6) / 4453 closed (+38) / 5217 total (+44) RFE : 150 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 391 open ( +7) / 3028 closed (+12) / 3419 total (+19) Bugs : 906 open ( -3) / 5519 closed (+19) / 6425 total (+16) RFE : 207 open...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 420 open ( +4) / 3410 closed ( +2) / 3830 total ( +6) Bugs : 915 open (+17) / 6186 closed ( +6) / 7101 total (+23) RFE : 235 open...
11
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.