reading txt file

Terry Reedy tjreedy at udel.edu
Thu May 31 13:23:08 EDT 2012


On 5/31/2012 11:02 AM, Ahmed, Shakir wrote:

When you want to start a new thread, make sure you start a new thread 
and do not post as a response to another thread. This will get lost as a 
response to 'How to suppress ...'.

Is this a homework, hobby, or work exercise?

> I am trying to read a txt file and dump the columns in an access database table.

It is good that you posted the traceback. Some beginners omit that. But 
we usually also need to see the code that caused the problem -- but just 
that code. It is best if one can produce a minimal file that exhibits 
the problem.

>>>> Unhandled exception while debugging...
> Traceback (most recent call last):
>    File "C:\WindDuration\Reading_test.py", line 14, in<module>
>      my_length_1 = nmAddrList[1]
> IndexError: list index out of range
>
> Getting error when it is reading to second line.

 > The txt file is attached here

Attachments do not work well on lists. They get omitted from responses. 
First *3* lines, copy and pasted:

FL Dade       Existing       Existing       Existing       10/24 12E 
[05]  10/24 14E [07]  10/24 16E [09]  88kt 10/24 07

FL Monroe     Existing       Existing       Existing       10/24 11E 
[04]  10/24 12E [05]  10/24 15E [08]  94kt 10/24 07

I suspect your problem is with the blank line(s) between data lines, but 
I cannot tell for sure without your code. In any case, you have to skip 
them.

FL Desoto     Existing       Existing       Existing       10/24 09E 
[02]  10/24 12E [05]  10/24 15E [08]  67kt 10/24 08

FL Sarasota   Existing       Existing 
    10/24 11E [04]  10/24 15E [08]  59kt 10/24 07

This data file is column-oriented and has missing values indicated by 
blank space. So you cannot separate fields with the .split method. 
Instead, you have to slice out each field with a pair of indexes.

To facilitate column counting, I would put a temporary line like
01234567890123456789012345678901234567890123456789... at the top.
(I also still have a special 'document rule' meant for this purpose. It 
is marked in 1/10 inch increments to count characters printed in a fixed 
10 chars per inch font such as Courier.)

There is also text at the bottom after a separator line
------------------------------ ...
so you will have to catch that and stop there.

-- 
Terry Jan Reedy




More information about the Python-list mailing list