0

I am beginer in Python but I would like to convert a lot of csv file to dbf by python-dbf module. The csv files have different names and I want to get dbf with same name as csv. I tried to carry out this issue with for loop by the following way:

x1 = 'path/to/csv/a.csv'
x2 = 'path/to/csv/b.csv'
x3 = 'path/to/csv/c.csv'
y1 = 'a.dbf'
y2 = 'b.dbf'
y3 = 'c.dbf'
x = [x1,x2,x3]
y = [y1, y2, y3]
for z in range (0,3) : table = dbf.from_csv(x[z], y[z])

Is there any quicker method to solve this issue?

Beata
  • 335
  • 2
  • 10
  • 21

1 Answers1

1
def conv(file_path)
    file_name = os.path.basename(file_path)
    file_name_without_ext = os.path.splitext(file_name)[0]
    dbf_file_name = file_name_without_ext + ".dbf"
    return dbf_file_name

def dbf_from_csv(f):
    dbf.from_csv(f, conv(f))

# slow version
for f in x:
    table = dbf.from_csv(f, conv(f))

# quick version - use multiprocessing:
from multiprocessing import Pool

p = Pool() # see docs for how many processes this will launch
p.map(dbf_from_csv, x)
p.close()
p.join()
scytale
  • 12,346
  • 3
  • 32
  • 46
  • Your code is cleaner, but you are using a `for` loop just like the question. – Ethan Furman Aug 20 '15 at 13:19
  • Dear Scytale, Thank you for your comment. Could you please write me which module need for file conversion? I tried conv(file_path) but I got syntax error. – Beata Aug 21 '15 at 06:52