3

I would like to convert my excel file into dbf file using python.

sample input file emp.xlsx is as below

enter image description here

I went through the below links and tried.

convert csv file to dbf

Convert .csv file into .dbf using Python?

dbf file is created but all the dbf file contents are MEMO only. I need above data as it is in dbf file also. But all are MEMO only. see the below screenshot of output.dbf

enter image description here

I used below python script

import pandas as pd
import dbf

# convert list into string
def list_to_string(mylist): 
    str1 = "," 
    return (str1.join(mylist))

# read from xls file
data=pd.read_excel('emp.xlsx')

# convert column name list into string
myheader = list_to_string(data.columns)

# write to csv
data.to_csv('test.csv',index=None,header=True)

# convert csv into dbf with header
dbf.from_csv(csvfile='test.csv', filename='output.dbf',field_names=myheader.split(','),  to_disk=True)

test.csv file content open through excel application

id  name       phone       dob
101 Joseph  3883474834  10-12-2020
102 Kaif    5454564644  14-12-2020
103 John    2344553354  17-12-2020

test.csv contents open through notepad

id,name,phone,dob
101,Joseph,3883474834,2020-12-10
102,Kaif,5454564644,2020-12-14
103,John,2344553354,2020-12-17
Ravi Kannan
  • 303
  • 1
  • 3
  • 11
  • Can you post the first three lines from the 'test.csv' file? – Ethan Furman Jan 23 '22 at 02:38
  • I edited my above post with output of test.csv – Ravi Kannan Jan 23 '22 at 04:04
  • Thanks, but you'll need to load the csv file into a text editor so we can see what's actually in it. Whichever program you used already did a bunch of converting so we can't see the actual data in the file. – Ethan Furman Jan 23 '22 at 05:43
  • Thanks for your comments. There is no package to directly convert from excel file to dbf. dbf package has function to convert from csv to dbf. You can see the created test.csv in the same path. csv file is proper. If you open csv through notepad, it is showing all data as separated with comma. I opened test.csv through notepad, and pasted the contents in the above post. I made below steps 1) Excel file data imported as dataframe 2) From dataframe test.csv is created. You 3) From csv i tried to convert into dbf using dbf.from_csv function. – Ravi Kannan Jan 23 '22 at 06:31
  • Thanks for the update. the `.from_csv()` method simply loads all data as memo fields as it refuses to guess at data types. The linked answer has better instructions for your goal. – Ethan Furman Jan 23 '22 at 17:00
  • @EthanFurman, is this answer, https://stackoverflow.com/a/6673379/246801, still relevant? It seems to imply the opposite of your answer in the post you marked this question a duplicate of. – Zach Young Jan 23 '22 at 17:23
  • @ZachYoung: It is relevant if all you need is the data as strings (maybe for searching, maybe because your data is already only strings, etc.). If you have non-string data in the csv file, and need it to have the same non-string data types in the dbf file, then the answer I linked to has the proper instructions for that. – Ethan Furman Jan 23 '22 at 17:27

0 Answers0