AttributeError:“NoneType”对象没有属性“closed”Python

2024-09-30 01:24:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,需要将一些.iff数据转换为.shp、.shx和.dbf数据。我将下面的代码与JoelLawhead提供的pyShape包结合使用。我得到一个错误:“…第157行,在 如果不是w.shx.close:w.shx.close() AttributeError:“非类型”对象没有属性“已关闭”。这似乎是一个简单的错误,解决起来可能并不复杂,但对我来说很难。也许有人能帮我

向维斯塔斯问好

###Code####
from __future__ import print_function
import shapefile
import struct, sys

sys.path.insert(1,r"C:\Python27\ArcGIS10.6\Lib\pyshp-1.2.3")
#sys.path.insert(1,r"C:\Python2_7\Lib\pyshp-1.2.3")
#import shapefile

# Constants for shape types
#NULL = 0
#POINT = 1
#POLYLINE = 3
#POLYGON = 5
#MULTIPOINT = 8
#POINTZ = 11
#POLYLINEZ = 13
#POLYGONZ = 15
#MULTIPOINTZ = 18
#POINTM = 21
#POLYLINEM = 23
#POLYGONM = 25
#MULTIPOINTM = 28
#MULTIPATCH = 31

In = "VOR_1"

IFFin = In + ".IFF"
shpfile_out = In

try:
    print ("In: IFF-file:   " + IFFin)
    print ("Out: Shapefile: " + shpfile_out + ".shp")


# Calculate MaxTime
    
    prev_part = 0
    MaxTime = []
    MaxTime.append(1)
    MaxTime[0] = 0
    i = 0
    j = 0
    IFFfile = open(IFFin, "r")
    for line in IFFfile:
        i = i + 1
        if i == 1:
            t = line.split(" ")
            NrAtt = int(t[0])
            if NrAtt != 9:
                print ("Number of attributes in IFF-file <> 9")
                print ("Programm aborted...")
                break
        elif i > 10:
            while "  " in line:
                line = line.replace ("  "," ")
            t = line.strip(" ").split(" ")
            part = int(t[0])
            if prev_part != part:
                MaxTime.append(1)
                j = j + 1
            prev_part = part
            MaxTime[j] = float(t[5])
            
    IFFfile.close()



    Shape_Type = 5
    w = shapefile.Writer(shapefile.POLYLINE)
    w.autoBalance = 1
    w.field('Particle','N',6)
    w.field('ILAY_Start','N',6)
    w.field('Z_start','F',10,8)
    w.field('TIME_start','F',10,8)
    w.field('Veloc_start','F',10,8)
    w.field('IROW_start','N',6)
    w.field('ICOL_start','N',6)
    w.field('RTIME_start','F',10,8)
    w.field('ILAY_end','N',6)
    w.field('Z_end','F',10,8)
    w.field('TIME_end','F',10,8)
    w.field('Veloc_end','F',10,8)
    w.field('IROW_end','N',6)
    w.field('ICOL_end','N',6)
    w.field('RTIME_end','F',10,8)
    w.field('Z_Mean','F',10,8)
    w.field('TIME_Mean','F',10,8)
    w.field('Veloc_Mean','F',10,8)
    w.field('RTIME_Mean','F',10,8)
    
    
    i = 0
    j = 0
    prev_part = 0
    ilay_start = 0
    xcrd_start = 0
    ycrd_start = 0
    zcrd_start = 0
    time_start = 0
    veloc_start = 0
    irow_start = 0
    icol_start = 0       
    rtime_start = 0

    IFFfile = open(IFFin, "r")
    for line in IFFfile:
        i = i + 1
        if i == 1:
            t = line.split(" ")
            NrAtt = int(t[0])
            if NrAtt != 9:
                print ("Number of attributes in IFF-file <> 9")
                print ("Programm aborted...")
                break
        elif i > 10:
            while "  " in line:
                line = line.replace ("  "," ")
            t = line.strip(" ").split(" ")
            part = int(t[0])
            ilay_end = int(t[1])
            xcrd_end = float(t[2])
            ycrd_end = float(t[3])
            zcrd_end = float(t[4])
            time_end = float(t[5])
            veloc_end = float(t[6])
            irow_end = int(t[7])
            icol_end = int(t[8])            

            if prev_part == part:
                w.line(parts=[[[xcrd_start,ycrd_start],[xcrd_end,ycrd_end]]])
                w.record(part,
                         ilay_start, zcrd_start, time_start, veloc_start, irow_start, icol_start, (MaxTime[j] - time_start),
                         ilay_end, zcrd_end, time_end, veloc_end, irow_end, icol_end, (MaxTime[j] - time_end),
                         ((zcrd_start + zcrd_end) / 2), ((time_start + time_end) / 2),
                         ((veloc_start + veloc_end) / 2),
                         (((MaxTime[j] - time_start) + (MaxTime[j] - time_end)) / 2))
            else:
                j = j + 1
            prev_part = part
            ilay_start = ilay_end
            xcrd_start = xcrd_end
            ycrd_start = ycrd_end
            zcrd_start = zcrd_end
            time_start = time_end
            veloc_start = veloc_end
            irow_start = irow_end
            icol_start = icol_end

    IFFfile.close()
    w.save(shpfile_out)
  

finally:

    if not IFFfile.closed : IFFfile.close()
    if not w.shp.closed : w.shp.close() #if not w.shp.closed : w.shp.close()
    if not w.shx.closed : w.shx.close()
    if not w.dbf.closed : w.dbf.close()
    print('%s' % 'End')

###Code end###

Tags: fieldcloseiftimelinestartintend

热门问题