简体   繁体   English

是否有从 netCDF4 文件中提取文件路径的特定方法?

[英]Is there a specific way to extract file paths from netCDF4 files?

Whenever I extract file names from a netCDF4 file, the individual letters come out straddled by a 'b' and apostrophes.每当我从 netCDF4 文件中提取文件名时,单个字母就会出现在“b”和撇号之间。 So the filepath "/home/data" looks like: [b'/' b'h' b'o' b'm' b'e' b'/' b'd' b'a' b't' b'a']所以文件路径“/home/data”看起来像:[b'/' b'h' b'o' b'm' b'e' b'/' b'd' b'a' b't' b '一种']

Does anyone know why this is happening or how to convert this to a filepath that looks normal?有谁知道为什么会发生这种情况或如何将其转换为看起来正常的文件路径?

I am extracting the filepath with the following code:我正在使用以下代码提取文件路径:

filepath = '/Users/jchap/Desktop/img_tags.nc'
with nc4.Dataset(filepath,'r') as ncid:
    L1fileName = ncid.variables['L1fileName'][:]
print(L1fileName[0])

And then receive the following mishmash results in lieu of a filepath:然后接收以下混合结果代替文件路径:

[b'/' b'h' b'o' b'm' b'e' b'/' b'd' b'a' b't' b'a' b'w' b'o' b'r' b'k'
 b'-' b'c' b'e' b'r' b's' b'a' b't' b'-' b'p' b'u' b'b' b'l' b'i' b'c'
 b'/' b'c' b'a' b'c' b'h' b'e' b'/' b'p' b'r' b'o' b'j' b'e' b'c' b't'
 b'/' b's' b'1' b'a' b'-' b'w' b'v' b'1' b'-' b's' b'l' b'c' b'-' b'v' 
 b'v' b'-' b'2' b'0' b'0' b'2' b'f' b'0' b'9' b'b' b'-' b'0' b'1' b'3' 
 b'.' b't' b'i' b'f' b'f']

Note that the data "class" is: <class 'numpy.ma.core.MaskedArray'>请注意,数据“类”为:<class 'numpy.ma.core.MaskedArray'>

Thanks for the help!谢谢您的帮助!

s = [b'/' b'h' b'o' b'm' b'e' b'/' b'd' b'a' b't' b'a' b'w' b'o' b'r' b'k'
 b'-' b'c' b'e' b'r' b's' b'a' b't' b'-' b'p' b'u' b'b' b'l' b'i' b'c'
 b'/' b'c' b'a' b'c' b'h' b'e' b'/' b'p' b'r' b'o' b'j' b'e' b'c' b't'
 b'/' b's' b'1' b'a' b'-' b'w' b'v' b'1' b'-' b's' b'l' b'c' b'-' b'v' 
 b'v' b'-' b'2' b'0' b'0' b'2' b'f' b'0' b'9' b'b' b'-' b'0' b'1' b'3' 
 b'.' b't' b'i' b'f' b'f']

s = s.replace("b\'", '').replace("[","").replace("']","")
print(s)
[out]: '/home/datawork-cersat-public/cache/project/s1a-wv1-slc-vv-2002f09b-013.tiff'

The reason for this is briefly explained here: https://www.tutorialspoint.com/What-does-the-b-character-do-in-front-of-a-string-literal-in-Python原因在这里简要解释: https : //www.tutorialspoint.com/What-does-the-b-character-do-in-front-of-a-string-literal-in-Python

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM