python读取shp文件属性值

python读取shp文件属性值并保存到csv

使用shapefile库

pip安装shapefile

(E:\Anaconda3\install1) C:\Users\luo>
(E:\Anaconda3\install1) C:\Users\luo>
(E:\Anaconda3\install1) C:\Users\luo>activate wind_2021

(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>pip install pyshp
Collecting pyshp
  Downloading pyshp-2.1.3.tar.gz (219 kB)
     |████████████████████████████████| 219 kB 1.1 MB/s
Building wheels for collected packages: pyshp
  Building wheel for pyshp (setup.py) ... done
  Created wheel for pyshp: filename=pyshp-2.1.3-py3-none-any.whl size=37263 sha256=e33f542c2231f5821d9b5ed1364d4f6c237537789ac6570fd2e98b92ed9b109e
  Stored in directory: c:\users\luo\appdata\local\pip\cache\wheels\1f\1b\b5\54affbefc8a7e2bdf1da000fc576b8a1c91338f1f327a04f4c
Successfully built pyshp
Installing collected packages: pyshp
Successfully installed pyshp-2.1.3

(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
(wind_2021) C:\Users\luo>
# -*- coding: utf-8 -*-
import os
import shapefile
import csv

csvFile = open('2022011203.csv','w')
csvFileWriter = csv.writer(csvFile)

#csvData=[]

def closeCsvFile():
    csvFile.close()

def readShpFile(filePath):
    sf = shapefile.Reader(filePath,encoding='gb18030')
    shapes = sf.shapes()
    print(len(shapes))
    
    print(sf.numRecords)
    recds = sf.records()

    print("===========================")
    for i in range(sf.numRecords):
        index_record = sf.record(i)
        #print(index_record)
        print(index_record[4])

        row_data = []
        row_data.append(index_record[4])
        #csvData.append(row_data)
        csvFileWriter.writerow(row_data)
        
    print("===========================")


#遍历所有的.shp文件
def listShpFile():
    for root,dirs,files in os.walk(r"L:\1"):
        for file in files:
            filePath1 = os.path.join(root,file)
            #获取文件路径
            #print(filePath1)

            if filePath1.endswith('.shp'):
                print(filePath1)

                readShpFile(filePath1)

listShpFile()        
closeCsvFile()



#############

控制台输出:

(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>python read_shpFile.py
L:\1\云南省.shp
1
1
===========================
325.742766452
===========================
L:\1\内蒙古自治区.shp
5
5
===========================
678.534285197
448.038002689
92.534590756
253.868195245
127.464894164
===========================
L:\1\北京市.shp
1
1
===========================
133.037525083
===========================
L:\1\四川省.shp
5
5
===========================
460.767387631
251.79989182
258.688406106
215.078670498
254.617643929
===========================
L:\1\天津市.shp
1
1
===========================
60.8299320136
===========================
L:\1\宁夏回族自治区.shp
3
3
===========================
134.981531258
64.1409276783
144.013836245
===========================
L:\1\安徽省.shp
3
3
===========================
9.08142626514
58.9031666487
136.59494157
===========================
L:\1\山东省.shp
3
3
===========================
252.061179574
307.964969258
283.174542213
===========================
L:\1\山西省.shp
4
4
===========================
204.821593789
43.7189271288
97.3978018136
221.365985938
===========================
L:\1\广东省.shp
4
4
===========================
87.2235500153
45.4491173993
287.105881527
92.6199768563
===========================
L:\1\广西壮族自治区.shp
2
2
===========================
43.8509477048
313.613431782
===========================
L:\1\江苏省.shp
2
2
===========================
99.3899710804
313.582221405
===========================
L:\1\江西省.shp
4
4
===========================
4.46045752435
23.7189066167
145.828639557
290.066700568
===========================
L:\1\河北省.shp
4
4
===========================
88.1191342986
81.3578760568
324.936282346
300.166849237
===========================
L:\1\河南省.shp
3
3
===========================
247.166874523
40.4232758514
143.922760949
===========================
L:\1\浙江省.shp
1
1
===========================
175.65265493
===========================
L:\1\海南省.shp
1
1
===========================
3.87152968208
===========================
L:\1\湖北省.shp
5
5
===========================
254.81862358
335.350997361
86.2476743885
245.297373116
274.716598637
===========================
L:\1\湖南省.shp
5
5
===========================
95.4377463121
210.358797156
64.4810195597
114.00272542
96.6557220969
===========================
L:\1\甘肃省.shp
2
2
===========================
228.247395758
162.129809287
===========================
L:\1\贵州省.shp
3
3
===========================
101.182842926
189.398201755
22.1319744775
===========================
L:\1\重庆市.shp
6
6
===========================
163.723590071
128.192588581
90.5047252429
211.376101063
68.9162112196
262.67782227
===========================
L:\1\陕西省.shp
4
4
===========================
311.85785335
630.277081506
263.259355233
245.820319105
===========================
L:\1\青海省.shp
1
1
===========================
164.33228919
===========================
L:\1\黑龙江省.shp
1
1
===========================
129.45378885
===========================

(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>
(wind_2021) L:\>

##########################

QQ 3087438119
原文地址:https://www.cnblogs.com/herd/p/15791295.html