1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| #coding:utf-8
import dbfread
from dbfpy import dbf
import itertools
import xlrd
import sys
def writeBmh(datas, filename, header):
db = dbf.Dbf(filename, new=True)
for kv in header:
# field = field.encode('GBK')
db.addField(kv)
idx = dict()
n = 0
for field in datas[0]:
idx[field] = n
n = n + 1
for record in datas:
if record[0] == 'bmh':
continue
rec = db.newRecord()
for kv in header:
value = record[idx[kv[0]]]
#value = value.encode('utf-8')
#print value, type(value)
#if type(value) == unicode:
if kv[1] == 'D':
value = value.replace('-', '')
elif kv[1] == 'C':
rec[kv[0]] = value.encode('GBK')
elif kv[1] == 'N':
rec[kv[0]] = int(value)
else:
rec[kv[0]] = value
rec.store()
db.close()
def readxls(filename):
data = xlrd.open_workbook(filename)
table = data.sheet_by_index(0)
datas = []
for rn in range(table.nrows):
datas.append(table.row_values(rn))
return datas
f1='mm.xlsx'
datas = readxls(f1)
h=[['bmh','C',10],['bmm','C',76],['oldbmh','C',10],['bm','C',30],['bz','C',50]]
o='m.dbf'
writeBmh(datas,o,h)
|