简体   繁体   English

类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 GeojsonFile

[英]TypeError: expected str, bytes or os.PathLike object, not GeojsonFile

datafile = open(filename, "r");数据文件 = 打开(文件名,“r”); TypeError: expected str, bytes or os.PathLike object, not GeojsonFile类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 GeojsonFile

can someone help me why this happen?有人可以帮助我为什么会发生这种情况吗? i want to read spatial data in geojson format.我想以 geojson 格式读取空间数据。

Act i want to store geojson data into the database.我想将 geojson 数据存储到数据库中。 I already connect the database which is MongoDB database.我已经连接了 MongoDB 数据库。 Im using gridfs to overcome the limitation, but first i need to read the data and store into the database.我使用 gridfs 来克服限制,但首先我需要读取数据并存储到数据库中。

many thanks非常感谢

    #read in the spatial data
filename = "/dami_data/data1/countries.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open(filename, "r")
thedata = datafile.read()

#create a gridFS object 
filesystem = gridfs.GridFS(db, collection='data')

#write data to GridFS
myfile = filesystem.put(thedata, filename = "countries")
print ("Store to database :)")

#retrieve what was just stored/document
filesystem.get(myfile).read()

In your code, filename refers to a GeojsonFile object instead of a filename.在您的代码中, filename指的是 GeojsonFile 对象而不是文件名。

If the file you want to read raw is the same file as the file pygeoj reads, then you should do:如果您要读取的原始文件与 pygeoj 读取的文件相同,那么您应该执行以下操作:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open(filename, "r")
thedata = datafile.read()
datafile.close()

Though you can do it, you should consider using geojfile instead of reading the file again.尽管您可以做到,但您应该考虑使用geojfile而不是再次读取文件。 It already holds information that you needed from that file anyways (as pointed out by @Karl Knechtel in the comments).无论如何,它已经包含您需要从该文件中获取的信息(正如@Karl Knechtel在评论中指出的那样)。

If not, meaning you want to read a different file, you should pass the filename:如果没有,意味着你想读取不同的文件,你应该传递文件名:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
datafile = open("my_other_file_name", "r")
thedata = datafile.read()
datafile.close()

Last case, if you would like to get the raw data of that GeojsonFile, do:最后一种情况,如果您想获取该 GeojsonFile 的原始数据,请执行以下操作:

filename = "/dami_data/test_data/roads.geojson"
geojfile = pygeoj.load(filename)
for feature in geojfile:
    print(feature.geometry.type)
    print(feature.geometry.coordinates)
thedata = str(geojfile)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pyqtdeploy : TypeError: 预期的 str、bytes 或 os.PathLike 对象,而不是 NoneType - pyqtdeploy : TypeError: expected str, bytes or os.PathLike object, not NoneType 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 int in - 子进程 - TypeError: expected str, bytes or os.PathLike object, not int in - subprocess 类型错误:预期的 str、字节或 os.PathLike object,不是 dict - TypeError: expected str, bytes or os.PathLike object, not dict 类型错误:预期的 str、字节或 os.PathLike object,不是列表转换 - TypeError: expected str, bytes or os.PathLike object, not list convert 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 DataFrame - Not a Repost - TypeError: expected str, bytes or os.PathLike object, not DataFrame - Not a Repost 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 None 类型 - TypeError: expected str, bytes or os.PathLike object, not None Type TypeError:预期的 str、字节或 os.PathLike 对象,而不是 Image - TypeError: expected str, bytes or os.PathLike object, not Image Django TypeError:预期的 str、bytes 或 os.PathLike object,不是 NoneType - Django TypeError: Expected str, bytes or os.PathLike object, not NoneType 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 FieldFile - TypeError: expected str, bytes or os.PathLike object, not FieldFile 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 ImageFieldFile - TypeError: expected str, bytes or os.PathLike object, not ImageFieldFile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM