简体   繁体   中英

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. 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']

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'>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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