[Tutor] Writing a txt from dbf
Steven D'Aprano
steve at pearwood.info
Tue Sep 28 23:56:26 CEST 2010
On Wed, 29 Sep 2010 04:33:51 am Susana Iraiis Delgado Rodriguez wrote:
> Hello dear pythonists:
>
> I'm developing an application in python, I'm new using this
> programming language I used to work with Java, but in my job my
> superiors suggested me to develop in this language.
> I'm trying to read a dbf file, I already done it but my code shows me
> all the lines without spaces, I want it toshow line per line and then
> write the lines into a plain txt file.
I am not sure what you mean. My guess is that you want something like:
alpha
beta
gamma
delta
in the file, but instead you get:
alphabetagammadelta
Am I right?
> My code is:
>
> from dbf import *
> from string import strip
There is no need for this any more, as the functions in the string
module are now also available as string methods. So instead of:
import string
print string.lower(my_string)
you can write:
my_string.lower()
> import sys
> def demo1():
> a = open ("archivo.txt","w")
> dbf = Dbf('tapalpa_05_plani_point.dbf',new=False)
As a matter of style, it is normal to use 4 spaces for indents, not 1.
You are welcome to use whatever you like in your own code, but many
people find 1 space indents hard to see and so when writing for others
(such as when asking a question here) you should use at least 2 spaces.
> for k in dbf:
> print '%s'%(strip(k[2]))
The print command automatically adds a newline after the string, so each
printed string should be on its own line. But later, when you write the
string to the file, you must add the newline yourself.
> l=()
> l=(strip(k[2]))
> a.write(l)
There's no need to clear l with the line l=() first. Just write:
l = k[2].strip()
a.write(l + '\n')
--
Steven D'Aprano
More information about the Tutor
mailing list