473,378 Members | 1,140 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,378 software developers and data experts.

read a files particular line

Hi,
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)

thanks for your help

Frank Grellert
Jul 18 '05 #1
5 1999
Hi,

If the file is not too big use:

file("filename").readlines()[11]

Kristóf

Frank Grellert wrote:
Hi,
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)

thanks for your help

Frank Grellert


Jul 18 '05 #2
Kristóf Stróbl wrote (top-posting, but I fixed it. You're welcome.):
Frank Grellert wrote:
does anybody have an idea how to read a particular line of a file
like awk's awk '{if( NR==12) print $0}' filename (reads lineno 12 of
filename)


If the file is not too big use:

file("filename").readlines()[11]


And if the file is "too big", there's always iterators:

print itertools.islice(open('tempx.txt'), 11, 12).next()

A little more complicated, but avoids reading the entire file
into memory.

-Peter
Jul 18 '05 #3
Hello Frank,
Hi,
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)

Check the documentation about the "linecache".

HTH.
--
------------------------------------------------------------------------
Miki Tebeka <mi*********@gmail.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys
Jul 18 '05 #4
Frank Grellert <fg*******@t-online.de> writes:
Hi,
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)


for lineno, line in enumerate(open_file):
# lineno starts at 0, though

Cheers,
mwh

--
<radix> I feel so special when people quote me
-- from Twisted.Quotes
Jul 18 '05 #5
Frank Grellert <fg*******@t-online.de> wrote:
does anybody have an idea how to read a particular line of a file like awk's
awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)


Standard Python library module linecache, which others suggested, is
your best bet. itertools.islice(open('filename'), 12, 13).next() should
also work (after an 'import itertools' of course, since it, too, is a
module in the standard library) and might be faster if you'll never need
to get any other line from that file in the course of your program.
Alex
Jul 18 '05 #6

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

Similar topics

6
by: sea | last post by:
I have text files in the following format: 123,34, ,345,890, 123,23 .. .. .. As you can see, the problem is that (1) the commas can occur in
0
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
4
by: sudhakaranr | last post by:
HI, Need your help please... First thing, i want to open a directory and with in the directory i have 1000's of files and we need to read the file and i want a particular line which has...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.