471,339 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Python; jump to a concrete line

Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?

Thanks
Dec 20 '07 #1
5 3341
On Dec 20, 7:56 pm, Horacius ReX <horacius....@gmail.comwrote:
Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?

Thanks
If you are working with a file that is of a fixed length you can use
the .seek() function
ie. 500 characters per line, and a newline character is an additional
one then for the 10th line you would do:

file_input.seek(5010, 0)
file_input.readline()

If it is not a fixed length file you can do:

line_to_seek = 9 # for the 10th line, you need to look for num9 as
counting starts @ 0
for (i, line) in enumerate(file_input):
if i == line_to_seek:
.... process the line

As for combatting the issue of lack of data on a line, do something
like:

line_contents = line.split(' ')
if len( line_contents ) = required_elements:
.... do something
else:
.... print 'failed'
Dec 20 '07 #2
"Horacius ReX" <ho**********@gmail.comwrote in message
news:5c**********************************@p69g2000 hsa.googlegroups.com...
Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?
Have you considered using the file.readlines() method?

Russ

Dec 20 '07 #3
On Dec 20, 8:13 pm, "Russell Blau" <russb...@hotmail.comwrote:
"Horacius ReX" <horacius....@gmail.comwrote in message

news:5c**********************************@p69g2000 hsa.googlegroups.com...
Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?

Have you considered using the file.readlines() method?

Russ
..readlines() will hog some resources on large files.
file.read().splitlines()[ line_to_seek ]
might be the fastest way for small files, but also will swell for
larger ones.
Also, one line way to get what you want:
[a for (i,a) in enumerate(input_file) if i == line_to_seek]
Dec 20 '07 #4
Horacius ReX <ho**********@gmail.comwrites:
Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?
Consider using linecache:
http://docs.python.org/lib/module-linecache.html

HTH,
Rob
Dec 20 '07 #5
Horacius ReX wrote:
Hi, sorry but after looking for information, I still did not get how,
when reading a text file in python, can one jump to a concrete line
and then read the different data (separated by spaces). In each line
there is different number of columns so sometimes i get kind of "index
out" error. Is there a better way to read the different data on each
row and avoiding to know the exact number of columns ?

Thanks
There may be a language barrier here (probably on my side), but
I think that the OP was asking for something more elementary.

You have:
DATA DATA
DATA DATA DATA DATA
DATA DATA DATA
DATA DATA
DATA DATA
DATA DATA DATA
DATA DATA
DATA DATA
Read the file and 'jump' to line four, and then do:

line.split()[3]

and get "kind of 'index out' error"

So he could at least do:

l = line.split()
if len(l) index:
data = l[index]
else:
data = None

Assuming index counts from zero.

Or perhaps I miss the point entirely.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
7 posts views Thread by Kyler Laird | last post: by
29 posts views Thread by Maurice LING | last post: by
1 post views Thread by Jerald | last post: by
36 posts views Thread by Andrea Griffini | last post: by
137 posts views Thread by Philippe C. Martin | last post: by
18 posts views Thread by stylecomputers | last post: by

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.