当批量读取csv,TypeError:预期的str,字节或os.PathLike对象,而不是int

时间:2019-05-15 11:52:27

标签: python-3.7

我不知道要批量读取csv,但是我希望我能读取它,现在我已经编写了一个函数,但是我不能使用它

我还没有找到解决问题的方法,您只需要阅读“ def”部分,了解我输入的内容以及代码的结尾即可

def trans_point(folder, fn, idlng, idlat, delimiter=','):
    # create a point shapefile
    output_shp = shp.Writer(shp.POINT)
    # for every record there must be a corresponding geometry.
    output_shp.autoBalance = 1
    # create the field names and data type for each.you can omit fields here
    # output_shp.field('id','N') # number    
    output_shp.field('longitude', 'F',9,6) # float
    output_shp.field('latitude', 'F',9,6) # float
    output_shp.field('value','F',9,6) # string, max-length
    # access the CSV file
    with codecs.open(folder + fn, 'r', 'utf-8') as csvfile:
        reader = csv.reader(csvfile, delimiter=delimiter)
        # skip the header
        next(reader, None)
        #loop through each of the rows and assign the attributes to variables
        for row in reader:
            # idx = row[0]
            value= row[2]
            lng= float(row[idlng])
            lat = float(row[idlat])
            print(lng, lat)
            # create the point geometry
            output_shp.point(lng, lat)
            # add attribute data
            output_shp.record(lng, lat, value)
    output_shp.save(folder + "%s.shp"%fn.split('.')[0]) # save the Shapefile

if __name__ == '__main__':
    folder = 'H:\pr_rain\data_3d_1' + os.sep
    fn = 'test.csv'
    trans_point(folder, fn, 1, 0)

0 个答案:

没有答案