On 01/19/2013 05:25 AM, kiwi64ajs wrote:
> Hi
Hello!
> I'm trying to read library book data from a Visual FoxPro 3 DBF file using Python and the dbf library.
>
> Here is the code I'm trying:
> <pre>
> from dbf import *
> print Table("book.dbf")
> for rec in Table("book.dbf"):
> print rec
> </pre>
`Table` returns a table every time you call it. You should save the
result from one call so you're not creating several links to the same
file as that can result in data corruption.
Here's what you should do:
from dbf import *
book_data = Table("book.dbf")
book_data.open()
for rec in book_data:
print rec
book_data.close()
if you are using a Python that supports `with` you could also do it this
way:
from dbf import *
book_data = Table("book.dbf")
with book_data:
for rec in book_data:
print rec
and `book_data` will be automatically opened and closed for you.
A couple notes you may not be aware of: `from whatever import *` should
only be used with modules/packages that were designed for it -- if they
don't say they support it, assume they don't (mine is); even if the
module/package does support it, you might be better off just importing
the pieces you need:
from dbf import Table, List
> I get the book.dbf fields printed out (see below) ok (two fields are in cp874 charsset) [...]
Hopefully /all/ the fields are in cp874 -- if not, you may have to do
some extra coding to properly decode the fields that aren't.
> Any help would be much appreciated.
Let me know if you get stuck anywhere else.
~Ethan~