Home > database >  How to convert a .shp and .dbh file to an excel spreadsheet using python?
How to convert a .shp and .dbh file to an excel spreadsheet using python?

Time:10-04

I have downloaded a Zip file containing files(.dbf, .shp) from the website using python.

    from zipfile import ZipFile
    with ZipFile('.\ZipDataset\ABC.zip','r') as zip_object:
       print(zip_object.namelist())
       zip_object.extract('A.dbf')
       zip_object.extract('B.shp')

My question is how to convert the above extension file to excel spread sheet using python?

CodePudding user response:

You need an additional libraries like pandas , pyshp or geopandas
According to this link the easiest way is :

You need to install 2 libraries

pip install pandas
pip install pyshp

and then run this code :

import shapefile
import pandas as pd
#read file, parse out the records and shapes
sf = shapefile.Reader('.\ZipDataset\ABC.zip')
fields = [x[0] for x in sf.fields][1:]
records = sf.records()
shps = [s.points for s in sf.shapes()]

#write into a dataframe
df = pd.DataFrame(columns=fields, data=records)
df = df.assign(coords=shps)
df.to_csv('output.csv', index=False)
  • Related