append whitespace & reindex issue

218 views
Skip to first unread message

Paul Manning

unread,
Sep 7, 2018, 12:42:55 PM9/7/18
to Python dBase
The problem I am having is with leading whitespace is being striped during append. The other issue is the database is not being reindexed. I am not using the last column of the csv file. The second column has two leading spaces. The issue is I need the space entered or the module that uses this database doesn't read the ratestep column correctly. The database is indexed on the first column (npanxx) assending. 

csv data:
629200,  1,TRUE
629201,  1,TRUE
629205,  1,TRUE
629207,  1,TRUE
629209,  1,TRUE
629210,  1,TRUE
629214,  1,TRUE
629218,  1,TRUE
629219,  1,TRUE
629220,  1,TRUE
629222,  1,TRUE
629223,  1,TRUE
629233,  1,TRUE
629335,  1,TRUE
629444,  1,TRUE
629772,  1,TRUE

f = open("areacodes.csv","r")
r = csv.reader(f)


db = dbf.Table('NPA0001.DBF')
db.open(mode=dbf.READ_WRITE)
# print(db('npanxx'))
# db.zap()
db.close()
for row in r:
    NPANXX_id = row[0]
    RATESTEP_id = str(row[1])
    RATESTEP_length = len(str(row[1]))+2
    RATESTEP_revised = RATESTEP_id.rjust(RATESTEP_length)
    AOS_id = 1
    db.open(mode=dbf.READ_WRITE)
    db.append(NPANXX_id,RATESTEP_id,AOS_id))


db.open(mode=dbf.READ_WRITE)
db.reindex()
db.close()
f.close()

dBase III database table:

Capture2.PNG


You can see the string data for column 2 has spaces on data it to the right of the column. 


Capture3.PNG


Thanks,
Paul

Ethan Furman

unread,
Sep 8, 2018, 2:57:31 PM9/8/18
to python...@googlegroups.com, Paul Manning
On 09/06/2018 08:19 PM, Paul Manning wrote:

> The problem I am having is with leading white space is being stripped
> during append. The other issue is the database is not being reindexed. I
> am not using the last column of the csv file. The second column has two
> leading spaces. The issue is I need the space entered or the module that
> uses this database doesn't read the ratestep column correctly. The
> database is indexed on the first column (npanxx) assending.

The dbf library does not support cdx index files. I don't think there
is a way to prevent the leading space stripping. It looks like your
first two columns would be better as integers (also making the white
space handling a non-issue).

--
~Ethan~
Reply all
Reply to author
Forward
0 new messages