472,954 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 3447
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Prabu | last post by:
Hi, I'm new to python, so excuse me if i'm asking something dumb. Does python provide a mechanism to implement virtual functions? Can you please give a code snippet also...:) Thanx in advance...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Kyler Laird | last post by:
I've been using Pyrex to get me through the Computer Vision class I'm taking this semester http://shay.ecn.purdue.edu/~ece661/ by dealing with the requirement that all programming assignments be...
29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
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...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
18
by: stylecomputers | last post by:
Hi All, What do you find the best IDE for creating web applications in Python is? Preferably FOS IDE. Cheers
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.